-1

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

Dnu_12
  • 45
  • 1
  • 10
  • 1
    You don't have to call onPause() manually, it will be called automatically once your activity goes in bg. Please post your code, then only we can help you to find the answer. – Shrikant Ballal Sep 17 '12 at 12:41
  • You are Not supposed to call these methods, android system calls them. – S.D. Sep 17 '12 at 12:42
  • Check out http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle If there's only one document you want to read on android, read this! – Pramod Sep 17 '12 at 13:23

3 Answers3

8

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.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • 1
    Almost same reply at the same time :P :D – Shrikant Ballal Sep 17 '12 at 12:42
  • 1
    indeed :) You should have posted that as an answer, too – WarrenFaith Sep 17 '12 at 12:45
  • I thought, If I post this as an answer, user's may downvote it, since it's more like a comment. But I should have done that, at least I would have earned some reps. :P :) – Shrikant Ballal Sep 17 '12 at 12:49
  • so how to pause an app and resume it? – Dnu_12 Sep 17 '12 at 12:52
  • Again,you don't have to worry about pausing an app and resume it. Android OS will handle it. – Shrikant Ballal Sep 17 '12 at 12:54
  • lets say, there you are playing a game! now you have to pause it in the middle! is it possible? – Dnu_12 Sep 17 '12 at 12:58
  • Consider using [DialogBox](http://developer.android.com/guide/topics/ui/dialogs.html) – Shrikant Ballal Sep 17 '12 at 13:08
  • @Dnu_12, you can save your application state by overriding the onSaveInstanceState() method, and restore it by overriding onRestoreInstanceState() – Vinay S Shenoy Sep 17 '12 at 13:23
  • 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. – WarrenFaith Sep 17 '12 at 13:29
  • yes Warren! Thanks! now I understand! I have a thread for the game logic, I'm pausing it, and it working fine, I have some part of the logic running in the onDraw method as well, which I guess I have to transfer to the game logic thread, I think you should post the last comment as a separate answer for me to tick it! – Dnu_12 Sep 17 '12 at 13:43
  • for later use, I merged my comment into the answer. Glad we could help. – WarrenFaith Sep 17 '12 at 13:51
1

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.

Robert
  • 1,272
  • 4
  • 21
  • 40
  • Hi! How can I save the activity and resume it? – Dnu_12 Sep 17 '12 at 12:51
  • You can use onSaveInstanceState() and onRestoreInstanceState(). Check also http://stackoverflow.com/questions/151777/saving-activity-state-in-android – Robert Sep 17 '12 at 12:52
  • That seems to work! But if i have lots of data to be passed that will be a problem for me :( – Dnu_12 Sep 17 '12 at 13:00
  • Basically we are using it to store activity state, values from edit test's and so on... from activity just for the time when it'll not be used... for other puropses you should use Sqlite database. What kind of data you man by "lots"?? – Robert Sep 17 '12 at 13:16
  • It seems that i have misunderstood onPause and onResume What i actually wanted to do is to pause my app with a button! it seems calling onPause should not be done for that purpose,, or is it the one? – Dnu_12 Sep 17 '12 at 13:21
  • What you mean by "pause application"? You want to put it into background? – Robert Sep 17 '12 at 13:27
  • No, Robert! lets say a user is playing a game and he want to pause it! well I did this by pausing the logic thread, But I have some of the logic part in my onDraw method which I'm now going to transfer to the logic thread as well,, it will do what I wanted,, thanks! – Dnu_12 Sep 17 '12 at 13:52
0

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.

Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45
  • this http://stackoverflow.com/questions/4553605/difference-between-onstart-and-onresume – Ankitkumar Makwana Sep 17 '12 at 13:00
  • it seems that the app is been paused by the OS and if i want to add a pause button to pause the app (what i wanted to do) i should not call onPause() within on click pause button? – Dnu_12 Sep 17 '12 at 13:07