27

The first Activity that loads in my application is an initialization activity, and once complete it loads a new Activity. I want to ensure if the user presses 'Back' they go straight to the Launcher, and not the initialization screen. Side note, is this even the best approach, or would this be better done with some kind of Intent Flag?

Is it correct to call finish() after calling startActivity() on the new activity?

onCreate() {
...
startActivity(new Intent(this, NextActivity.class));
finish();
...
}

I'm still taking in the whole 'Message Queue' method of doing things in Android, and my assumption is that calling startActivity() and then finish() from my first Activity's onCreate() will log each respective message in the message queue, but finish execution of onCreate() before moving on to starting the next Activity and finishing my first one. Is this a correct understanding?

stormin986
  • 7,672
  • 15
  • 42
  • 54

1 Answers1

29

Probably you should just use the noHistory flag on the activity in your manifest.xml

jqpubliq
  • 11,874
  • 2
  • 34
  • 26
  • Thanks! that accomplishes exactly what I wanted to do. I'm still interested, however, in hearing if there is anything inherently wrong or bad form in my previously described approach, and if my understanding of how messages are handled in the queue is correct. – stormin986 Apr 26 '10 at 01:04
  • 12
    Your code (start and then finish) is absolutely correct, and a technique that is often used. – hackbod Apr 26 '10 at 03:55
  • 2
    @hackbod: Could you please point to the documentation? Just in case someone needs it (like me)... –  Feb 25 '12 at 19:41
  • 1
    What if Activity A has nohistory=true and is first send to the background (i.e. User pressed the Home button) and then brought back to the foreground - would the state of Activity A have been preserved despite the noHistory=true? – AgentKnopf Apr 04 '12 at 06:37
  • Be careful with noHistory. New tasks will not auto-finish an activity, onPause() is called but not onStop and onDestroy. – Cookster Apr 08 '14 at 18:20
  • 6
    My personal experience has been that noHistory is rarely the right solution. One (usually) unexpected behavior is that if your users get an incoming call while on a noHistory activity it will disappear. – Nick Jul 03 '14 at 18:06