What am I doing Wrong?
Activity Flow:
HomeActivity
--> SettingActivity
--> ProfileActivity
In ProfileActivity
I have LogOut
button. OnClick
LogOut I doing...
public void onLogout()
{
//do this on logout button click
Intent intent = new Intent();
intent.setAction("com.package.ACTION_LOGOUT");
this.sendBroadcast(intent);
}
And in SettingsActivity
onCreate
I am doing this...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.package.ACTION_LOGOUT");
LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("onReceive","Logout in progress");
//At this point we should start the login activity and finish this one.
Intent gotoLogin = new Intent(SettingsActivity.this,Login_Activity.class);
startActivity(gotoLogin);
finish();
}
}, intentFilter);
}
I thought I will logout from the ProfileActivity
but I not.
What is wrong with this code.
Use intent flags to clear out your activity stack, refer this for more details https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK
So you need to implement your onLogout() method like below and you are good to go
public void onLogout() {
//do this on logout button click
Intent logoutUser = new Intent(context, Login_Activity.class);
logoutUser.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(logoutUser);
}
Note: Also clear your shared preference session or local databases related to user profile if you are using any of it