Reputation: 623
In my app, I want to prevent the user from going back to login activity after logging in, but I don't want to close the app. For example if the previous activity is login activity, I don't want to do anything when the user presses back, just stay in the current activity/fragment.
Upvotes: 4
Views: 5070
Reputation: 1
Just remove the super.onBackPressed like:
@Override
public void onBackPressed() {
//super.onBackPressed();
. . .
}
It will only detect when user clicks back button, and doesn't close the app.
Upvotes: 0
Reputation: 10910
The correct way to do this is to call finish()
in the Login activity when it completes and launches the next activity. For example:
Intent intent = new Intent(LoginActivity.this, NextActivity.class);
startActivity(intent);
finish();
This would prevent the user from going back from NextActivity
to LoginActivity
since calling finish()
destroys LoginActivity
and removes it from the back stack.
There is no need to remove onBackPressed
from NextActivity
, and that may have unintended consequences (for example, if they navigate from Login -> Next -> Other -> Next
then click back, they would expect to go back to Other
). Disabling onBackPressed
in Next
would prevent that.
Upvotes: 0
Reputation: 6277
Add android:noHistory="true"
in the manifest of your LoginActivity
<activity android:name=".LoginActivity" android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Upvotes: 0
Reputation: 295
add finish();
in login activity and add onBackPressed in your next activity
@Override
public void onBackPressed() {
}
Upvotes: 3