Android - Activity Life cycle

The android activity provides 7 call backs which will be invoked based on the states of the activity





For better undersatnding,The Activity A and Activity B with the all the call backs overridden.


ActivityA :

 public class ActivityA extends Activity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e("A-OnCreate", "Object Creation");

    }

    public void next(View v) {
        Intent i = new Intent(this, Main2Activity.class);
        startActivity(i);
    }

    @Override    protected void onStart() {
        super.onStart();
        Log.e("A-onStart", "View is visible but will not be able to interact");
    }

    @Override    protected void onResume() {
        super.onResume();
        Log.e("A-onResume", "View is visible and  will  be able to interact");
    }

    @Override    protected void onPause() {
        super.onPause();
        Log.e("A -onPause", "If starting new activity, the view is still visible but will not be able to interact");
    }

    @Override    protected void onStop() {
        super.onStop();
        Log.e("A- onStop", "If the activity is no longer visible");
    }

    @Override    protected void onRestart() {
        super.onRestart();
        Log.e("A- onRestart", "If the activity is restarted after onStop()");
    }

    @Override    protected void onDestroy() {
        super.onDestroy();
        Log.e("A- onDestroy", "If the activity is destroyed");
    }
}
ActivityB:


public class Main2Activity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("B -OnCreate","Object Creation");
    }

    @Override    protected void onStart(){
        super.onStart();
        Log.e("B-onStart","View is visible but will not be able to interact");
    }

    @Override    protected void onResume() {
        super.onResume();
        Log.e("B-onResume","View is visible and  will  be able to interact");
    }

    @Override    protected void onPause() {
        super.onPause();
        Log.e("B -onPause","If starting new activity, the view is still visible but will not be able to interact");
    }

    @Override    protected void onStop() {
        super.onStop();
        Log.e("B- onStop","If the activity is no longer visible");
    }

    @Override    protected void onRestart() {
        super.onRestart();
        Log.e("B- onRestart","If the activity is restarted after onStop()");
    }

    @Override    protected void onDestroy() {
        super.onDestroy();
        Log.e("B- onDestroy","If the activity is destroyed");
    }
}





Scenarios:


1. Launching Activity A

04-23 07:54:51.965 28098-28098/com.example.lenovo.androidlifecycle E/A-OnCreate: Object Creation
04-23 07:54:51.967 28098-28098/com.example.lenovo.androidlifecycle E/A-onStart: View is visible but will not be able to interact
04-23 07:54:51.969 28098-28098/com.example.lenovo.androidlifecycle E/A-onResume: View is visible and  will  be able to interact



2. Button click to move Activity B

04-23 08:00:56.131 28098-28098/com.example.lenovo.androidlifecycle E/A -onPause: If starting new activity, the view is still visible but will not be able to interact
04-23 08:00:56.143 28098-28098/com.example.lenovo.androidlifecycle E/B -OnCreate: Object Creation
04-23 08:00:56.146 28098-28098/com.example.lenovo.androidlifecycle E/B-onStart: View is visible but will not be able to interact
04-23 08:00:56.156 28098-28098/com.example.lenovo.androidlifecycle E/B-onResume: View is visible and  will  be able to interact
04-23 08:00:56.486 28098-28098/com.example.lenovo.androidlifecycle E/A- onStop: If the activity is no longer visible

Notice that onStop() of Activity A is called only after Activity is Visible and interactable (after onResume() of Activity B)


3. Pressing back button from Activity B to navigate to Activity A


04-23 08:18:42.459 28098-28098/com.example.lenovo.androidlifecycle E/B -onPause: If starting new activity, the view is still visible but will not be able to interact
04-23 08:18:42.470 28098-28098/com.example.lenovo.androidlifecycle E/A- onRestart: If the activity is restarted after onStop()
04-23 08:18:42.471 28098-28098/com.example.lenovo.androidlifecycle E/A-onStart: View is visible but will not be able to interact
04-23 08:18:42.472 28098-28098/com.example.lenovo.androidlifecycle E/A-onResume: View is visible and  will  be able to interact
04-23 08:18:42.809 28098-28098/com.example.lenovo.androidlifecycle E/B- onStop: If the activity is no longer visible
04-23 08:18:42.809 28098-28098/com.example.lenovo.androidlifecycle E/B- onDestroy: If the activity is destroyed



4. Pressing home button from Activity A

04-23 09:12:49.133 31302-31302/com.example.lenovo.androidlifecycle E/A -onPause: If starting new activity, the view is still visible but will not be able to interact
04-23 09:12:49.398 31302-31302/com.example.lenovo.androidlifecycle E/A- onStop: If the activity is no longer visible


5. Opening app (Activity A)after it went to Background

04-23 09:16:07.973 31302-31302/com.example.lenovo.androidlifecycle E/A- onRestart: If the activity is restarted after onStop()
04-23 09:16:07.974 31302-31302/com.example.lenovo.androidlifecycle E/A-onStart: View is visible but will not be able to interact
04-23 09:16:07.975 31302-31302/com.example.lenovo.androidlifecycle E/A-onResume: View is visible and  will  be able to interact


6. When the Activity A is in foreground and an incoming call is coming


04-23 10:06:45.966 28136-28136/com.example.lenovo.androidlifecycle E/A -onPause: If starting new activity, the view is still visible but will not be able to interact


7. After the incoming call is finished/ignored

04-23 10:08:39.410 28136-28136/com.example.lenovo.androidlifecycle E/A-onResume: View is visible and  will  be able to interact


8.If screen is locked when Activity A is in foreground

04-23 13:26:39.874 702-702/com.example.lenovo.androidlifecycle E/A -onPause: If starting new activity, the view is still visible but will not be able to interact
04-23 13:26:40.006 702-702/com.example.lenovo.androidlifecycle E/A- onStop: If the activity is no longer visible

and if the screen is unlocked

04-23 13:27:21.896 702-702/com.example.lenovo.androidlifecycle E/A- onRestart: If the activity is restarted after onStop()
04-23 13:27:21.944 702-702/com.example.lenovo.androidlifecycle E/A-onStart: View is visible but will not be able to interact
04-23 13:27:21.945 702-702/com.example.lenovo.androidlifecycle E/A-onResume: View is visible and  will  be able to interact


Please comment your queries


Comments

Popular Posts