3

I'm ware of androids life cycle and that it's not needed to add a "exit" button in the application.

But still, this back button stuff isn't really working out well.

You maybe know this issue from the default SMS-App that android comes with: you open it when you get a new message and exit it using the menu button or something else. After like 20 times doing this, you then decide to exit the app using the back button, what happens? you have to go back though 20 views. every time you press back you return to the "all messages (by sender)" listview and when you press back there again you return to the message opend 20-1 (message 19). again you press back and return to the listview and again you press back and end up at message 18. till, after40 times pressing back, you finally exit the messanger app.

same happens when for example you got a action bar with a "home" icon which opens the main screen of your app. the user picks a action and the new activity starts. than the user clicks the home button and returns to the main screen. when pressing the back button - no matter if you call finish() in the onButtonBack listener or not, you the user would expect the app to exit, but in fact the app returns to the previous activity which is wrong.

such cycleing may happen for various reasons, thats why - even thought i'm aware of the supposed to be lifecycle of android - i wan't to EXIT (& destroy) the app when pressing back within a defined activity.

calling finish() dosn't help. if there's a previous activity it will re-open it. calling system.exit(0) isn't nice to do.

so: whats the right way to prevent such back-press-cycles and/or exit a application (WITH destruction)?

for better illustration of what i want to achieve: consider A, B, C being activities. a arrow (-->) illustrations a new intent call from the activity leftside of the arrow, rightside of the arrow represents the activity that is called. ex.: A --> B means activity A starts activity B. now here's what i want:

  • 1) A --> B --> C pressBack:--> B pressBack:-->A pressBack:--> Exit
  • 2) A --> B pressBack: --> A pressBack: --> Exit
  • 3) A --> B --> A pressBack:--> Exit

as you see, back works as always, BUT when in activity A it exits the application.

the behaviour i got now is 1) and 2) as above but

  • 3) A --> B --> A pressBack:--> A pressBack: --> Exit

keep in mind, i've already overwritten the onBackPressed listener of activity A with a finish() call. even calling system.exit(0) dosnt work. however, even if it would: its not what i want, i want the REAL way to do it android style - i cant imagine system.exit(0) is best practise.

omni
  • 4,104
  • 8
  • 48
  • 67
  • I think this link must help you. This question is similar to yours. http://stackoverflow.com/questions/4732184/how-to-finish-an-android-application – superM Jan 31 '12 at 11:33
  • You can apply it in your AndroidManifest.xml file, just adding android:noHistory="true" attribute in those you want. – Nikhil C George Jan 31 '12 at 11:44
  • its not like i don't wont history at all. so android:noHistory="true" isnt doint it for me. but i dont want to have back-button-cycles. Example: I start the App, Activity A opens, A starts B, B starts A, A starts B. now if i press back i want to return to A ONCE. with the next press of the back button i want the app to exit. the "normal" behaviour is that you would return to B again, would need to press back once more, would return to A and finaly with one more back press you exit. with android:noHistory="true" you can't return to A from B - thats not what i want. – omni Feb 01 '12 at 18:25

1 Answers1

4

Well this is the default behavior.

If you have another approach, just implement it.

One approach to deal with this is to use the android:launchMode="singleInstance" for activities that can be launched in a singleton manner (only one activity can exist)

For example, if the SMS page in the SMS app was a singleTop, it would have needed only one back press to remove all the SMS pages. It is a matter of choice


Another more aggressive way would be to finish Activities when you start another activity. Of course, such decision would risk making the app less friendly (android users are not accustomed to this behavior). Nevertheless, if this is used only where it may be considered acceptable then it might be acceptable.

A very acceptable place to do this would be a login screen: Once login is successfull, you start another activity (probably designed for logged in users) and finish the login activity.


Enjoy Finally, in my personal opinion, you can add an Exit button. Users will find it nice.

Check my post: Adding an Exit button to Android Application

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • seems to be what i'm looking for. will try it out this evening and report back. thanks so far! – omni Jan 31 '12 at 12:11
  • i tried android:launchMode="singleTop", but that only makes sure you only got once instance of that activity. it dosn't prevent cycles caused by back button. expl. i have 2 activities A and B, both using singleTop. I start A and from there B and from there A again and from there B again. when pressing back i return the A, OK fine. then i press back again and return to (the same instance) of B --> wrong, thats not what i want! i want the app to exit when back is press while in A. using simple finish() within the onBack listener dosnt work. singleTop don't help anything here. – omni Feb 02 '12 at 17:00
  • owhhhh sorry @masi please try singleInstance instead of singleTop my fault – Sherif elKhatib Feb 02 '12 at 17:13
  • dosn't work as well. i added singleinstance to activity A. now the behaviour is even more strange. A --> B --> C --> A. From there: opening B now starts C. pressing back from within A goes back to B, pressing back again closes the app. – omni Feb 02 '12 at 20:42
  • yeah A will be found only one time... Is not that what you want? – Sherif elKhatib Feb 02 '12 at 21:21
  • improved initial question with more details – omni Feb 05 '12 at 23:56