There are a lot of topics on this post. But i couldn't find a solution to my problem.
Let me describe my activity
stack first.
SplashScreen->A->Login->Home.
What i would like to achieve is , when i click on back
button after logging in to Home
, i should come out of the application and go to Home
if i use my application again. For this i am assuming i should clear the activity stack before Home, after i login. I would also like to preserve the activity stack if the user hasn't logged in yet.
I want this to work on or after 2.1
What i have tried already.
using
finish()
inLogin
Activity , before callingstartActivity
onHome
. This will redirect me to A , if i use back button on Home.All variations of
FLAG_ACTIVITY_NEW_TASK
andFLAG_ACTIVITY_CLEAR_TOP
. Nothing worked, when i use开发者_StackOverflow中文版 back button, i am redirected to login screen.
Any suggestions or simple solution to achieve this?
using
finish()
inLogin
Activity , before callingstartActivity
onHome
. This will redirect me to A , if i use back button on Home.
ok so use finish on all the activities that you want them to be popped before calling startActivity
go to
Home
if i use my application again
Simply save your login parameters in SharedPreference
and from A startActivity Home directly if login successful.
You can also try to make use of BroadcastReceiver
aswell if you want to try that route.
In your "SplashScreen" and "A" activities, in the onCreate
method you can create and register and IntentFilter
and a BroadcastReceiver
like so:
Assuming you have a global variable called broadcastReceiver
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("ACTION_LOGIN");
this.broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
registerReceiver(broadcastReceiver, intentFilter);
Also don't forget to unregister your receiver in the onDestroy
method (this is to prevent memory leaks in the program):
@Override
protected void onDestroy() {
unregisterReceiver(this.broadcastReceiver);
super.onDestroy();
}
Now in your "Login" activity, once the user has successfully logged in, you can broadcast a message to all the registered receivers, which will finish those activites in the back stack:
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("ACTION_LOGIN");
sendBroadcast(broadcastIntent);
Your SplashScreen and A activities will now be finished.
精彩评论