Overriding Default Back Button Behavior In Android
I just recently learned how to clear the backstack in Android. I have two activities, one for login (LoginActivity) and one to use the application (MainActivity). It consists of a
Solution 1:
Sounds like you want to override the back button behavior to move the app to the background instead of doing the usual back button behavior.
You can do this by overriding onBackPressed
:
@Override
public void onBackPressed()
{
moveTaskToBack(true);
}
Solution 2:
You can use activity's method.
from activity,
onBackPress()
from fragment,
getActivity().onBackPress()
Solution 3:
You can start Luncher Application in your MainActivity's onBackPressed(),it seems you press the home button.
According to :Going to home screen programmatically
@Override
public void onBackPressed() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
Post a Comment for "Overriding Default Back Button Behavior In Android"