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 theLoginDialog. - From the
LoginDialog, I go to theLoginActivitywithout clearing the back stack.- This is because I want the user to be able to go back from the
LoginActivityto theStartupActivity.
- This is because I want the user to be able to go back from the
- From the
LoginActivity, I go to theHomeActivityand clear the back stack - Pressing back still goes back to the
StartupActivityondevices <= API 10
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.