On my Android App, I am having trouble with navigation. To create an account on my App, I have 3 activities: A1->A2->A3. When the A3 activity is validated, I'm going to activity A4 where I'm logged on. I would like to enable history navigation from A3 to A2 and from A2 to A1 but since I'm logged on (A4), I don't want the user to come back on any of A3,A2,A1 activities using the native android back button. If I set noHistory to true on A3, the logged on user on A4 activity will still be able to come back on A2 activity. If I set noHistory to true on all A1,A2,A3 activities, the user won't be able to come back even if he's not logged in...
Could someone show me what's the best way to do that ?
Thanks in advance !
So, I find your workflow quite clear and for that reason, it's quite easy to make something clean:
I would do something like this:
// --- I'm in your A4 activity, do not change anything for the other activities ----
boolean isUserLoggedIn;
// Modify the boolean when the user logs in
@Override
public void onBackPressed() {
if (isUserLoggedIn){
// Let's say you want the user to return at the device root menu at this point
new AlertDialog.Builder(this)
.setTitle("Really Exit?")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}).create().show();
}
}
else{
// Should return to your previous activities if you set the history to true (what I recommend strongly)
super.onBackPressed();
}
}
I wrote it quickly on Vi (and didn't test), so maybe you'll have some little errors but basically that's the idea.