artouiros
artouiros

Reputation: 3987

override onbackPressed() by another's activity default

I have 2 activities. MyMain MySecond I call MySecond activity in MyMain activity. And what i want, that when pressing back button in MySecond activity, it doesn't return to MyMain activity, but return to the screen where MyMain activity was called. I found a way to ovverride it, so it just opens home screen by this:

 @Override
 public void onBackPressed() {
      Intent setIntent = new Intent(Intent.ACTION_MAIN);
        setIntent.addCategory(Intent.CATEGORY_HOME);
        setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(setIntent); 
        return;
 }

Upvotes: 1

Views: 2037

Answers (1)

Sean Schulte
Sean Schulte

Reputation: 3965

You may want to try this:

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

That will move not only the current activity but all activities in your task (which would be your app) to the back of the activity stack. It should then return you to wherever you were before your MyMain activity started.

Upvotes: 2

Related Questions