On logout, clear Activity history stack, preventing back button from opening logged-in-only Activities
I can suggest you another approach IMHO more robust.
Basically you need to broadcast a logout message to all your Activities needing to stay under a logged-in status. So you can use the sendBroadcast
and install a BroadcastReceiver
in all your Actvities.
Something like this:
/** on your logout method:**/
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_LOGOUT");
sendBroadcast(broadcastIntent);
The receiver (secured Activity):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**snip **/
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.package.ACTION_LOGOUT");
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("onReceive","Logout in progress");
//At this point you should start the login activity and finish this one
finish();
}
}, intentFilter);
//** snip **//
}
Prevent back button after logout in android
override the onBackPressed
in your login activity, to do nothing..
public void onBackPressed() {
//do nothing
}
Android: Remove all the previous activities from the back stack
The solution proposed here worked for me:
Java
Intent i = new Intent(OldActivity.this, NewActivity.class);
// set the new task and clear flags
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
Kotlin
val i = Intent(this, NewActivity::class.java)
// set the new task and clear flags
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(i)
However, it requires API level >= 11.
How to prevent going back to the previous activity?
My suggestion would be to finish the activity that you don't want the users to go back to. For instance, in your sign in activity, right after you call startActivity
, call finish()
. When the users hit the back button, they will not be able to go to the sign in activity because it has been killed off the stack.
Android - Kill all activities on logout
You could use a BroadcastReceiver to listen for a "kill signal" in your other activities
http://developer.android.com/reference/android/content/BroadcastReceiver.html
In your Activities you register a BroadcastReceiver
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("CLOSE_ALL");
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// close activity
}
};
registerReceiver(broadcastReceiver, intentFilter);
Then you just send a broadcast from anywhere in your app
Intent intent = new Intent("CLOSE_ALL");
this.sendBroadcast(intent);
Related Topics
Difference Between App:Srccompat and Android:Src in Android's Layout Xml
Android Read Text Raw Resource File
Android: Coloring Part of a String Using Textview.Settext()
Change Screen Orientation Programmatically Using a Button
Android - Set Max Length of Logcat Messages
How to Start Genymotion Device
Firebase Firestore Variable Name Changed
Adb Not Finding My Device/Phone (Macos X)
How to Return to a Parent Activity Correctly
How to Use Opencv in Using Gradle
How to Store(Bitmap Image) and Retrieve Image from SQLite Database in Android
Using Intent in an Android Application to Show Another Activity
How to Use Jni Bitmap Operations for Helping to Avoid Oom When Using Large Images
How to Define a Min and Max Value For Edittext in Android