1

if a user tapped the home button and open the app after that, how to not allow back? eg don't allow users to go back to screens that they seen before tapping the home button. It should be treated as a new session

user903772
  • 1,554
  • 5
  • 32
  • 55

3 Answers3

2

This sounds like a bad idea, as it blatantly goes against the Android task/navigation guidelines.

The user expects to be able to back out to the previous screen after resuming a task... and preventing it will potentially piss off a lot of users.

Please, please, please read these documents before you risk destroying the user experience.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • this is a preachy comment, not an answer. – Sam Dozor Jun 19 '12 at 15:46
  • 5
    It's also something that beginners will come across while searching for hacky ways to manipulate how the user navigates the application. I don't care if it's better suited as a comment... there are way too many crappy apps on the Android Market right now and it's because *people don't emphasize the importance of following Android conventions*. – Alex Lockwood Jun 19 '12 at 15:58
1

The home button cannot be overridden nore should it, if you dont want the user to go back to the activity they left when the home button was clicked then on the on pause of the activity just pop the backstack to where you want to be.

see this answer

Community
  • 1
  • 1
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • What if the user turns off the screen? Then `onPause` will be called and the backstack will be popped without the user ever touching the home button. – Alex Lockwood Jun 19 '12 at 15:35
  • I know it will, but then again what he is trying to accomplish isnt exactly right either which is why I up-voted your answer :) – tyczj Jun 19 '12 at 15:36
  • i can think of a number of use-cases where his request is quite valid. what if there was banking/credit card info on the screen and you always required a user to re-auth and start over? what if it was some game/activity that didn't want to you cheat and needed you to always start from the beginning? – Sam Dozor Jun 19 '12 at 15:39
  • Also, using some creativity and registering for the screen off broadcast, he could probably get tyczj's solution working. – Sam Dozor Jun 19 '12 at 15:45
  • Actually, you can Override the Home Button - http://stackoverflow.com/questions/5547818/can-i-override-home-button-on-my-own-home-screen-from-my-application – MrEngineer13 Jun 19 '12 at 16:01
  • @MrEngineer13, the accepted answer for that post is incorrect. See hackbod's post here: http://stackoverflow.com/q/6319060/844882 – Alex Lockwood Jun 19 '12 at 16:09
  • actually no you cannot, that key event does not get called. try it out before you say something works because it does not – tyczj Jun 19 '12 at 16:09
  • @Sam_D, if you were writing a banking application, you would not be writing anything specific to the press of the "home" button. Instead, you would be implementing something specific to the act of exiting a task (via an email notification, etc.). This is usually accomplished with the [`clearTaskOnLaunch`](http://developer.android.com/guide/topics/manifest/activity-element.html#clear) attribute in the Android manifest. The OP asks something much more specific... the examples you provided should be thought of in terms of "exiting a task", not "pressing the home button". – Alex Lockwood Jun 19 '12 at 16:13
0

If you want to end your Activity once it is no longer visible then finish your activity in your Activities call to onStop().

@Override
    protected void onStop() {
        super.onStop();

    this.finish();
}

This will finish your Activity with no chance of onRestart() being called. Be careful with this method because users expect to resume the app instead of having to start over, but there are cases where this would be the accepted protocol. See more on Navigation and the Activity LifeCycle.

Edit: Also see the question Android-Quittings an application - is that frowned upon? specifically this answer.

Community
  • 1
  • 1
MrEngineer13
  • 38,642
  • 13
  • 74
  • 93