-1

First time making an app in Android Studio. Its a simple webview app. I have a problem in which when i press the back button the app closes when i would like to return to previous page. How do i do that?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
D.Tomic
  • 1
  • 3

1 Answers1

0

Pressing back returns you to the previous Activity by default. If you only have 1 Activity then pressing back forces your app to close.

Act1 -----> Act2 If you are in Act2 and press back you will go back to Act1

Act1 If you only have Act1 and press back your app will close

If you want to change what pressing back does, you should override the onBackPressed method in your activity. Let's say that when you are in Activity2 and press Back, you want to go to Activity4. You should then include the following method in your Activity2 :

@Override
    public void onBackPressed() {

            Intent new_intent = new Intent(this, Activity4.class);

            this.startActivity(new_intent);

    }

Get it? Best of luck!

Edit: Ok i just saw your edit saying it's a webview. Nevermind then.