onPause() and onStop() in Activity

前端 未结 6 1356
借酒劲吻你
借酒劲吻你 2020-12-04 16:05

I am new to Android development and I am still not able to understand the onPause() and onStop() methods in an activity.

In my app, I have

6条回答
  •  半阙折子戏
    2020-12-04 16:43

    I know your question was 6 months ago but in case someone else stumbles on this question:

    am I doing something wrong to get my activity into the paused state.

    Yes, you are. This:

    I hit the home button (not the back button) of the emulator, and launch another app, believing that this would mimic onPause() activity.

    Hitting the home button will indeed call the onPause() method but because the home button makes your activity no longer visible it will then call the onStop() method (like patriot & milter mentioned).

    As per the Activities developer reference (http://developer.android.com/guide/components/activities.html) you can display a dialog or simply put the device to sleep.

    Alternatively, you call an activity that will only partially obstruct the calling activity. So call an activity that creates a window with a view of size:

     android:layout_width="100dp"
     android:layout_height="100dp"
    

    Which doesn't cover the entire screen, thus leaving the calling activity behind partially visible, thus calling only calling activity's onPause() method.

    Clone that activity so that both view sizes are "match_parent" instead of "100dp" and call it and both the onPause() and onStop() methods of the calling activity will be called because the calling activity won't be visible.

    There can be exceptions of course, like if the called activity causes an app crash in either of its onCreate(), onStart() or onResume() then the onStop() of the calling activity will not be called, obviously, I'm just talking about the general case here.

提交回复
热议问题