Andriod programming – Posts 10: Activity Lifecycle

What activities are also principles, Its sequence and too Activity. Activity is a very very important part of an Android application, Hence you should know and need to know about its activities. Activity behavior of such a cycle, so we call it life cycle of Activity.

[qads]

activity_lifecycle

Above are pictures illustrating the operation of 1 Activity. When the activity is opened up it will turn the function call onCreate (functions that make it), followed by onStart, onResume. After the Activity function onResume will officially operate and we can manipulate with as click button Activity,…

  • OnPause function is called in 2 TH later:
    +/ When you open an Activity but as dialog, Activity that is currently on top of another Activity still see the other part of Activity. Dialog does not apply to current expenditure that applies to existing as Activity Dialog.
    +/ When you open an app or other other Actiivty from the list of recent apps. When this function is called and immediately onPause will then call OnStop.

  • From this pause state can move to Activity 3 different states:
    +/ Callback function to return onResume run normally when the dialog are lost
    +/ Call to function OnStop when we do not call another dialog that called Activity. That is called an Activity from this Activity or have a certain program can overwrite it without seeing it again, it will call over 2 function is onPause and OnStop. When referred to the status of Activity OnStop is stop.
    +/ App was transferred to the state, and the callback function processes Killed onCreate. This case occurs when the amount of RAM and no place mandatory systems kill the program, Activity is not used in the present time.

  • From Stop status, ie after the call is also OnStop 3 possibility.
    +/ You turn off the current program or activity that override its current, it will call back function onReStart, then call onStart, onResume and back to normal activities.
    +/ App was transferred to the status process Killed, same as above.
    +/ Call to OnDestroy and canceled Activity, Activity occurs when you turn off the system off or due to lack of RAM or something error.

The theory is really not easy to imagine, so you Create New Project and create additional practice 2 new Activity. Ie we have 3 Activity offline.

android-activity-life-cycle-1

In particular Second Activity will be displayed as a dialog, to do this, you edit the file as follows AndroidManifest:

<activity
android:name=".SecondActivity"
android:theme="@style/Theme.AppCompat.Dialog"></activity>

Main Activity interface will look like this. We have 2 button to display the Dialog and Activity. From there we will look to see what happens.

android-activity-life-cycle-2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.nguyenvanquan7826.tut9activityliftcycle.MainActivity">

    <Button
        android:id="@+id/btnShowDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Dialog" />

    <Button
        android:id="@+id/btnShowActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Show Activity" />
</LinearLayout>

Follow, in MainActivity.

package com.nguyenvanquan7826.tut9activityliftcycle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        System.out.println("Main - onCreate");

        setContentView(R.layout.activity_main);

        connectView();
    }

    private void connectView() {
        findViewById(R.id.btnShowDialog).setOnClickListener(this);
        findViewById(R.id.btnShowActivity).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnShowDialog:
                showDialog();
                break;
            case R.id.btnShowActivity:
                showActivity();
                break;
        }
    }

    private void showDialog() {
        startActivity(new Intent(this, SecondActivity.class));
    }

    private void showActivity() {
        startActivity(new Intent(this, ThirdActivity.class));
    }

    @Override
    protected void onStart() {
        super.onStart();
        System.out.println("Main - onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("Main - onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        System.out.println("Main - onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("Main - onStop");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        System.out.println("Main - onRestart");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.out.println("Main - onDestroy");
    }
}

To write functions onStart, onPause,… fast, you put the mouse on the position to write, press Ctrl + The, then find the need to write a function that function by typing. For example, it wants to overwrite your jaw open Ctrl onPause + The, then write onPause that will see.

Here's a video illustrating the results offline.