Reputation: 9590
I have a login-activity which I only want to show on startup and if the user logs out. So when the user is in the loggedin activity and clicks on the back-button I want to close the app rather than go back to the login activity.
I have successfully overwritten the back button callback:
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back)
{
Console.Out.WriteLine("Close activity");
return true;
}
return base.OnKeyDown(keyCode, e);
}
which stops the backbutton from going back to the previous activity. However, including Finish();
before return true
does make the login activity reappear :(
This is the code I use after login:
Intent activity = new Intent(this, typeof(WorkOrderActivity));
StartActivity(activity);
Have tried a couple of Intentflags and LaunchModes but cant get it to work. According to the logs only one activity is started after login.
Upvotes: 0
Views: 4308
Reputation: 1643
It's the normal behaviour to show the previous Activity
after finish()
The finish()
method just close the Activity which call it
You can hide your login form and check the user login status on your onResume()
login Activity (and finish it if he is logged)
Upvotes: 2
Reputation: 5258
Try this it works..
Intent mIntent = new Intent(this, WorkOrderActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mIntent);
finish();
Upvotes: 3
Reputation: 2210
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back)
{
moveTaskToBack(true);
return true;
}
return base.OnKeyDown(keyCode, e);
}
Upvotes: 0
Reputation: 644
You should clear stack when starting another activity from Login activity.
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
And when user logout you can start LoginActivity.
Upvotes: 0