basics of android activity life cycle functions

社会主义新天地 提交于 2019-12-23 22:49:00

问题


I was testing out this code which shows which state an activity is in

public class Activity101Activity extends Activity {
    String tag  =  "Lifecycle";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);
        setContentView(R.layout.activity_activity101);
        Log.d(tag , "In the onCreate() event");
    }
    public void onStart()
    {
        super.onStart();
        Log.d(tag , "In the onStart() event");
    }

    public void onRestart()
    {
         super.onRestart();
        Log.d(tag , "In the onRestart() event");
    }

    public void onResume()
    {
         super.onResume();
        Log.d(tag , "In the onResume() event");
    }

    public void onPause()
    {
         super.onPause();
        Log.d(tag , "In the onPause() event");
    }

    public void onStop()
    {
         super.onStop();
        Log.d(tag , "In the onStop() event" );
    }

    public void onDestroy()
    {
         super.onDestroy();
        Log.d(tag , "In the onDestroy() event");
    }
}  

so I see that onDestroy() is only called when we press the back button while the activity is on screen, and is never called otherwise. So it should be running in the back ground if I press the home button while the activity is running. However, if I go to Settings -> Apps -> Running I can't see it on the list. So does that mean it is running in the background or not?

Again, Again, this code shows that onPause() is always followed by onStop() and onStart() is always followed by onResume(). So why are they defined as different functions in the Android environment and not combined?


回答1:


Once the activity goes in backstack, it is in suspended mode. So you dont see it in running app list. Once you relaunch such suspended application, it comes to foreground from backstack and starts to run. It is kept in backstack to preserve its state and resume from the place where it got stopped before going in background.

To understand why, onStart is needed before onResume follow the link below. It will clear all your doubts very clearly:

Difference between onStart() and onResume()




回答2:


Here I give you biref idea of activity life cycle...

onCreate():

Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by onStart().

onRestart():

Called after your activity has been stopped, prior to it being started again. Always followed by onStart()

onStart():

Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

onResume():

Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it. Always followed by onPause().

onPause ():

Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume(). When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

onStop():

Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.

Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.

onDestroy():

The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

So..onStart method call when activity comes in the foreground without interaction of user...and onResume method call when user starts interaction...so the functions of all methods is different ..

And once you press home button or back button your app running in the background and once you kill it from task manager it will shows nothing in the settings...




回答3:


onStart isn't always followed by onResume. onStart is called when you Activity is visible, and onResume is called when your Activity is active. For example, an Activity could be visible but not active if there was a dialog partially covering it.




回答4:


Thats' because there's a chance that onStart might not get called even when onResume actually is, there's a scenario when the activity might go to onPause only, without going to onStop [it only happens when activity is partially visible, not completelly covered by another activity], hence when comming back to activity only onResume will be called and not onStart.

Same with onPause, its always executed before onStop but onStop might not be called after onPause, as mentioned before, it could be called without onStop being actually called. If you press HOME you will not see the behavior i just explained because the activity is not partially visible, in order to reproduce it you have to find a way to make another component come on top without completely covering the activity...

Hope this helps.

Regards!



来源:https://stackoverflow.com/questions/18325654/basics-of-android-activity-life-cycle-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!