0

I have two layouts, A and B. The app launches the A_layout, and through a button you can go to the B_layout. On default when you press the back button the app closes, doesnt matter if the app is on the A or B layout. When I override the back button to set the content view always on the layout A whenever the back button is pressed, then I cant open the B activity anymore through the button. How do I need to override the method correctly? :)

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            setContentView(R.layout.activity_main);

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

activity_main = A layout Do I need to make Intents there?

Zombo
  • 1
  • 62
  • 391
  • 407

1 Answers1

0

You can override the onBackPressed function:

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(getApplicationContext(), ActivityA.class);
    startActivity(intent);
    finish();
}

When finsihing the activity is destroyed

M0CH1R0N
  • 756
  • 9
  • 19
  • do you also know how to check the active layout? with an if statement? –  Aug 10 '15 at 21:18
  • Check this out: https://stackoverflow.com/questions/11411395/how-to-get-current-foreground-activity-context-in-android – M0CH1R0N Aug 10 '15 at 21:40