How to redirect multiple types of users to their respective Activities?
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 **//
}
How to check if the user is logged in, if so show other screen?
Finish all previous activities
Use:
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities on top of home.
Assuming you are finishing the login screen when the user logs in and home is created and afterward all the screens from 1 to 5 on top of that one. The code I posted will return you to home screen finishing all the other activities. You can add an extra in the intent and read that in the home screen activity and finish it also (maybe launch login screen again from there or something).
I am not sure but you can also try going to login with this flag. I don't know how the activities will be ordered in that case. So don't know if it will clear the ones below the screen you are on including the one you are currently on but it's definitely the way to go.
Related Topics
Java Properties - How to Create Dynamic Properties
Springboot - How to Start Embedded Container
Saveastextfile() to Write the Final Rdd as Single Text File - Apache Spark
Map Json String Column of a JPA Entity to Java Object Automatically
How to Use Different Jsonproperty on Serialize & Deserialize Using Jackson API
How to Loop Through List of Webelements and Select One Webelement With a Condition
Unit Testing Private Functions in Junit With Mockito
How to Retrieve Element Value from Soap Response Using Java
Gson Expected Begin_Array But Was String At Line 1 Column 62
Sharing a Variable Between Multiple Different Threads
In Activity.Oncreate(), Why Does Intent.Getextras() Sometimes Return Null
Program With Threads for Matrix Multiplication
Parse a Uri String into Name-Value Collection
Spring Data Query Method With Optional @Param
Java.Time.Format.Datetimeparseexception: Text Could Not Be Parsed At Index 3