Willing to develop your own apps? Here i will bring you some basics about it. For me, the most basic thing before doing an android apps is knowing how the screen can be populated with fancy UI like in whatsapp, facebook, etc and destroy it.
Android will do couple of actions from you pressed your apps icon till it’s closed again by pressing back or home. In android apps, screen content will be managed by Activity class or its subclass like AppCompatActivity. Android Application can be consisted of more than one activity. Each activity will isolate its state and data from others except you shared them in Application Context. Everything inside application context can be shared between activities in same context (but there’s a better way to do this by using intent).
As described above, everything that shown in user’s screen will depends on activity class, so let’s figure it out how android manage the UI from activity. The one activity’s basic is to know its lifecycle. Activity entered its life the same time it need to shown to the screen and destroyed when it removed from screen. As described here, activities will have at least 4 essential state, but here i will provide you more than it because without fully knowing it’s state, I doubt that you can create a stable apps that give a good UX. Basic flow of android lifecycle can be seen in figure below.
- onCreate()
- onStart()
- onResume()
- onPause()
- onStop()
- onDestroy()
- onRestart()
- onSaveInstanceState()
- onRestoreInstanceState()
Then, how to trace back the state of our activity? The easiest way might be by use Log inside every state callback inside your activity like :
@Override
protected void onRestart() {
Log.i(TAG, "entering on restart");
super.onRestart();
}
@Override
protected void onStart() {
Log.i(TAG, "entering on start");
super.onStart();
}
Here i provide you with 4 scenarios :
- app open, app closed
- app open, app minimized, app open, app closed
- app open, app killed (in low ram then an interrupting task come such as phone call), app open, app closed
- app open, app minimized, app killed due to low memory, app open, app closed
Scenario #1 (app open, app closed)
This supposed to be most positive case happened, the result is straightforward as figure above
09-22 08:19:56.461 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on create
09-22 08:19:56.601 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on start
09-22 08:19:56.601 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on resume
#EXIT APPS
09-22 08:20:15.241 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on pause
09-22 08:20:15.821 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on stop
09-22 08:20:15.821 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on destroy
Scenario #2 (app open, app minimized, app open, app closed)
Quite similar with scenario #1, but when we pressed home button, onPause followed by onSaveInstanceState and onStop. onSaveInstanceState will give you chance to store any state, variable of your activity. But later when it’s called again, it will enter activity from onRestart which is not called savedInstance variable. So no need to worry for this scenario that your activity data lost. (We will discuss it in Scenario #3)
09-22 08:21:21.851 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on create
09-22 08:21:21.871 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on start
09-22 08:21:21.871 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on resume
# HOME BUTTON PRESSED
09-22 08:21:30.581 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on pause
09-22 08:21:31.141 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on save instance state
09-22 08:21:31.151 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on stop
# BACK INTO APPS
09-22 08:21:36.541 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on restart
09-22 08:21:36.551 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on start
09-22 08:21:36.551 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on resume
# HOME BUTTON PRESSED
09-22 08:21:41.281 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on pause
09-22 08:21:41.821 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on save instance state
09-22 08:21:41.821 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on stop
# BACK INTO APPS
09-22 08:21:48.801 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on restart
09-22 08:21:48.801 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on start
09-22 08:21:48.801 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on resume
# EXIT APPS
09-22 08:21:50.471 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on pause
09-22 08:21:50.991 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on stop
09-22 08:21:50.991 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on destroy
Scenario #3 (app open, app killed (in low ram then an interrupting task come such as phone call), app open, app closed)
Open -> let task manager kill it in background
Similar with Scenario #2, but if it killed in background, then when we enter the activities again, it will start from onCreate which will contain savedInstance data inside it and you may restore activity’s state from it
09-22 08:27:31.361 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on create
09-22 08:27:31.391 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on start
09-22 08:27:31.391 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on resume
# HOME BUTTON PRESSED
09-22 08:27:41.481 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on pause
09-22 08:27:42.021 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on save instance state
09-22 08:27:42.021 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on stop
# SOMEHOW SYSTEM KILLS IT
# BACK INTO APPS
09-22 08:27:55.221 11545-11545/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on create
09-22 08:27:55.351 11545-11545/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on start
09-22 08:27:55.361 11545-11545/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on resume
# EXIT APPS
09-22 08:21:50.471 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on pause
09-22 08:21:50.991 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on stop
09-22 08:21:50.991 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on destroy
Scenario #4 (app open, app minimized, app killed due to low memory, app open, app closed)
Open -> somehow task manager kill it in front of your eyes (it might happened on orientation change event)
This one quite different with others, instead go through onCreate, it will enter onRestoreInstanceState too. Those callback have savedInstance in it and you can restore Activity’s state using them. Usually onCreate will only use saveInstance for your main data, and onRestoreInstanceState will contain user input data in field etc so they won’t lose them. But those depend on your design.
09-22 08:27:31.361 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on create
09-22 08:27:31.391 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on start
09-22 08:27:31.391 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on resume
# SOMEHOW SYSTEM KILLS IT AND RESTART APPS
09-22 08:28:26.211 11545-11545/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on pause
09-22 08:28:26.211 11545-11545/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on save instance state
09-22 08:28:26.221 11545-11545/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on stop
09-22 08:28:26.221 11545-11545/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on destroy
09-22 08:28:26.231 11545-11545/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on create
09-22 08:28:26.261 11545-11545/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on start
09-22 08:28:26.271 11545-11545/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on restore instance state
09-22 08:28:26.271 11545-11545/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on resume
# EXIT APPS
09-22 08:21:50.471 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on pause
09-22 08:21:50.991 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on stop
09-22 08:21:50.991 1149-1149/com.felixsu.saa I/com.felixsu.saa.MainActivity: entering on destroy
To check it by yourself, you may install the apps here, then watch the logcat. To make life easier, set Log Level to INFO and put a filter such as activity name or fragment name into logcat
Leave a Reply