简体   繁体   中英

The lifecycle methods onPause and onStop

I am new to android. I have been working on it for one week. Unfortunately, I cannot handle the difference between the lifecycle methods onPause() and onStop() . Actually, I read some resources about these methods, but I cannot estimate which one is called when the cases listed below occurred.

  1. Pressing the home button of the smartphone when the activity X is demonstrated on the screen

  2. The smartphone enters into the sleep mode when the activity X is demonstrated on the screen

  3. Some smartphones can show more than application window on the screen. When only the activity X is demonstrated on the screen, I get the window of the activity X smaller and open second application. The windows of two applications are demonstrated on the screen of the phone.

I am really sorry about asking this question, but I cannot overcome this.

Is there anyone who can say which one of these two methods is executed for these scenarios and explain the difference between these methods?

Like many starters, you might have done an error of Jumping off random tutorials. Android Developers is the place that you should have started with. It have guides , codelabs and many other resources .

That being said, your question about lifecycles is well explained in the respective Guide .

onPause()

The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed); it indicates that the activity is no longer in the foreground (though it may still be visible if the user is in multi-window mode).

And then...

onStop()

When your activity is no longer visible to the user, it has entered the Stopped state, and the system invokes the onStop() callback. This may occur, for example, when a newly launched activity covers the entire screen. The system may also call onStop() when the activity has finished running, and is about to be terminated.

Apart from those differences, the OnStop will always follow OnPause and when activity is recovering from OnStop , it will go to the OnRestart first and continue from there while OnPause will start with OnResume when activity get resumed. See the diagram below for more information.

If you are still confused, let me know what your confusion still is in comments

图片来源:Android开发者网站

Let me help you with a simple manner:-

Lets assume we are going to use Whatsapp app:

When you click on the app ,the methods that will be called are:

onCreate()

onStart()

onReady()

Lets now assume, you pressed home button, and the app got minimized, the methods that will be called are:

onResume()

onPause()

onStop()

And now lets assume now you are going back to the minized Whatsapp app, methods that will be called are:

onRestart()

onStart()

Now lets assume, you just closed the app, and removed it from task manager, the methods that will be called are:

onStop()

onDestroy()

Here Is a diagram to get the sequence:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM