1

I have an application that has a startup flow like this:

StartupActivity -> LoginDialog -> LoginActivity -> HomeActivity

When I go from the LoginActivity to the HomeActivity I call:

Intent intent = new Intent( this, HomeActivity.class );
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB )
{
    intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK );
}
else
{
    intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
}
startActivity( intent );
finish();

On >= API 11, this makes it so that the HomeActivity is a brand new task and the StartupActivity is no longer on the back stack.

However, on API <= 10, the FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP clears the LoginActivity from the back stack, but the StartupActivity is still there. If the user clicks back from the HomeActivity, it takes them back to the StartupActivity.

How can I clear the StartupActivity from the back stack?

  • From my research, it seems like the best way is to startActivityForResult() but can I do that from the Dialog? Who will get the result?

Summary:

  • From the StartupActivity, I show the LoginDialog.
  • From the LoginDialog, I go to the LoginActivity without clearing the back stack.
    • This is because I want the user to be able to go back from the LoginActivity to the StartupActivity.
  • From the LoginActivity, I go to the HomeActivity and clear the back stack
  • Pressing back still goes back to the StartupActivity on devices <= API 10
zedman
  • 21
  • 5

2 Answers2

1

I figured out how to do it...

When the LoginDialog starts the LoginActivity, I can use startActivityForResult() to make the LoginActivity pass back a result to the StartupActivity. If the result is a successful login, I can finish the StartupActivity.

The LoginDialog can be triggered from multiple places, but I can set a flag if it's triggered from the StartupActivity in order to call startActivityForResult().

The thing I was stuck on was how to use startActivityForResult from a dialog to get the result back into the activity that launched the dialog... and I figured that out.

zedman
  • 21
  • 5
0

Add following in your AndroidManifest.xml file

android:noHistory="true"

for all the activities which you want to remove from backstack. You must put it inside the <activity> tag.

Or just call finish on StartupActivity as soon as you start a new activity.

I would suggest you the first option as its a right way to do it.

Cheers.

Take a look at this answer -> Answer

Community
  • 1
  • 1
Varundroid
  • 9,135
  • 14
  • 63
  • 93
  • Unfortunately, this doesn't work because I *do* want history for the StartupActivity. I want you to be able to go back to it from the LoginActivity, but I don't want you to go back to it anymore after you successfully get to the HomeActivity. – zedman Feb 14 '13 at 02:53