I havean Activity_A which has a Log-out option. On clicking that I call the Log-in Activity. So when i Click device back button when i am on Login-in Activity it takes me back to the Activity_A where i have the log-out option. what i wanted is, when i click device back button when i am on log-in Activity it must take me to the device home screen. something like "close application" or "clear activity stack".
I tried this:
Intent intent = new Intent(Activity_A.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
but it didn't work, i also tried using finish() int the above code, then it takes me to the Activity i called before Activity_A (as expected).
I searched for my case, and could not find a perfect solution, can someone please help me achieve what i wanted?
and i have an idea of overriding device back button onClick in my login Activity so that it shows the home screen of the device and clears all the activity stack....is it possible? if yes, is it safe?
This is what i have done in LoginActivity, which works just like i wanted
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
But is there any other way to achieve this without overriding onBackPressed()??
You need to override the back
button event and perform all the desired operation there.
@Override
public void onBackPressed() {
//do whatever you desire
return;
}