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?
Asked
Active
Viewed 1,573 times
-1
-
3Please post some code so we can see your two "pages", there's many ways to handle this. – CodeChimp Jul 11 '18 at 10:26
-
are you using fragments? – Mayank Bhatnagar Jul 11 '18 at 10:26
-
Whats a fragment? – D.Tomic Jul 11 '18 at 10:35
-
page means activity or fragment? – Radesh Jul 11 '18 at 10:36
-
Its a webview app. – D.Tomic Jul 11 '18 at 10:38
1 Answers
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.

Sotiris S. Magionas
- 759
- 6
- 25