-2

I got a problem with my app when the screen turns off (because of system screen timeout) my application finishes. I search but didn't find something helpful. Is it a common problem or does it have a fix?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Ibrahim Radwan
  • 95
  • 3
  • 11
  • 2
    can u post onPause code of ur activity? Also see logcat for any errors and post, if any – con_9 Feb 07 '14 at 21:19
  • 1
    Yeah I noticed now, my onPause contains finish() but I still want it in the onPause func Because I want when the user leaves this activity to finish but not the screen thnx – Ibrahim Radwan Feb 07 '14 at 21:21
  • 1
    I used ((Activity)context).finish(); in the next class to close original activity thnx all :) – Ibrahim Radwan Feb 15 '14 at 15:47

2 Answers2

2

you shud not call finish() in onPause. it could be called for variety of reasons(check doc.). why you want to kill your activity when user switches app? its not recommended. here are some posts, but there is no api available to detect app going in background.

How to detect when an Android app goes to the background and come back to the foreground

http://nathanael.hevenet.com/android-dev-detecting-when-your-app-is-in-the-background-across-activities/

Community
  • 1
  • 1
con_9
  • 2,491
  • 3
  • 23
  • 31
0

I want when the user leaves this activity to finish but not the screen thnx

Take finish() out of onPause(). Put it wherever the user leaves the Activity. So, assuming you have code that starts a new Activity, put finish() after startActivity().

You also could use the flag android:noHistory in your <activity> tag of your maifest.xml so that the Activity is removed from the stack whenever it starts a new Activity. Both of these methods do the same job it just depends on how you want/need to implement it.

The reason it closes when the screen turns off is because your app calls onPause() at that time so removing the call from that method would keep it from closing when the screen turns off.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • -1. Why would anyone put finish right after startActivity(...) call? Its not even related to whats asked. – con_9 Feb 07 '14 at 21:38
  • @con_9 it would finish the current `Activity`. It is directly related because the OP wants to close the current `Activity` when going to another one. That is why the `finish()` call is currently in the `onPause()` but calling it after `startActivity()` would finish it when going to another but not when the screen turns off. I do appreciate you leaving a comment with your downvote, however misguided it may be. – codeMagic Feb 07 '14 at 21:43
  • OP wants that current running activity should die if user puts the app in Background. Its not that when going to 2nd activity 1st one should die. if that was required, then just an intent flag wud have sufficed. – con_9 Feb 07 '14 at 21:57
  • OP only wants it to close when going to another activity. Not when being paused because the screen turns off – codeMagic Feb 07 '14 at 22:02
  • @codeMagic Your are right And this way you said I used to use it in my app but it sometimes closes and sometimes not so I put my code inside onPause but now I should remove it from this function – Ibrahim Radwan Feb 08 '14 at 11:23
  • @hemoali I can't say why it isn't working all the time without seeing the code that doesn't work but see my edit. – codeMagic Feb 08 '14 at 14:05
  • @con_9 if you read the previous comment by the OP you will see that my assumption of what is wanted here is correct and my answer is completely valid. There are many times when calling `finish()` after starting another `Activity` would make sense. And yes a flag could do the same thing but it depends on how the dev needs/wants the app to run – codeMagic Feb 08 '14 at 14:07