Skip to main content

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 from this blog

Circular Seek Bar - Example

MyView.java package com.rakesh.androidcircularseekbar; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class MyView extends View {     /** The context */     private Context mContext;     /** The listener to listen for changes */     private OnSeekChangeListener mListener;     /** The color of the progress ring */     private Paint circleColor;     /** the color of the inside circle. Acts as background color */     private Paint innerColor;     /** The progress circle ring background */     private Paint circleRing;   ...

SSL pinning in Android - A brief discussion

HTTP protocol Communication between the client and a server typically non-encrypted or plain text while we use HTTP protocol.  Pitfall Any middle hacker can interrupt the connection between the client and server and manipulate the data as it involves no encryption. How to overcome this ? As the domain owner one can purchase a digital certificate from CA(Certificate Authority) who are considered as trusted.  A certificate will contain the Owner's name, public key , Issuer's(CA's) name,Issuer's(CA's) signature, domain details, expiry date etc . After the SSL/Leaf certificate is associated with a domain,the communication between client and server will be encrypted. Now the HTTP will become HTTPs. Note : Associating the SSL certificate means it enable the encryption between client and server but does not mean ,the domain owner will never misuse your personal information. How does SSL work ? Pitfall There is a problem here. Let's assume that there is a hacker comes i...