Android activity.onPause() then activity.onResume() restarts the app

血红的双手。 提交于 2019-12-25 19:22:15

问题


I can successfully call activity.onPause() to pause the application. But when i call activity.onResume() the application restarts.

Is there a way to avoid this ?

Edited

I actually want to pause my application using a pause button


回答1:


You should never call methods of the Activity life cycle by yourself! So no call to onPause() onResume() onDestroy() onCreate() onStop() or onStart().

Edit to fit your edited question:

You should pause your game, not the Activity. You must have a thread where you work your game logic. That thread needs to be paused, not the game. So consider working on a pause logic there.




回答2:


As I can guess no there isn't. All activites are using following flow: http://developer.android.com/images/activity_lifecycle.png

You should save activity state and than resume it.

And as mentioned above you shouldn't call it yourself... just override/implement it and let Android do the job.




回答3:


see Activity life cycle like as

protected void onCreate(...) {
   // do Your work here
}

protected void onStart() {
   //       do Your work here
}

protected void onResume() {
        //  do Your work here
}

protected void onPause() {
               //  do Your work here
}

protected void onStop() {
               //  do Your work here
}

protected void onDestroy() {
              //  do Your work here
}



protected void onRestart() {
               //  do Your work here
}

under stood this method and You should save activity state and than resume it.



来源:https://stackoverflow.com/questions/12459347/android-activity-onpause-then-activity-onresume-restarts-the-app

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