Android activity.onPause() then activity.onResume(

2019-09-22 05:39发布

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

3条回答
我只想做你的唯一
2楼-- · 2019-09-22 06:15

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.

查看更多
来,给爷笑一个
3楼-- · 2019-09-22 06:17

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.

查看更多
闹够了就滚
4楼-- · 2019-09-22 06:19

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.

查看更多
登录 后发表回答