1

I have an activity contains WebView and I'm passing URL while opening, Like this:

Intent activity = new Intent(getApplicationContext(),NewActivity.class);
activity.putExtra("URL", url);
startActivity(activity);

When we press back button it closes all previous activities and return to MainActivity so I overrided onBackPressed.

@Override
    public void onBackPressed() {
        finish();
    }

and it still close all previous activities.

Note : It declared in manifest like this:

<activity
            android:name=".views.activities.NewActivity"
            android:screenOrientation="portrait"/>

What is the wrong with it?

How can i fix this, I just want to open previous activity onBackPressed and finish current one.

7
  • Have you tried Intent activity = new Intent(CurrentActivity.this, NewActivity.class);? Try with CurrentActivity.this instead of getApplicationContext() Commented Sep 7, 2016 at 8:08
  • Do you call finish() on your first activity after starting the NewActivity? Commented Sep 7, 2016 at 8:16
  • Sorry but using CurrentActivity.this instead of getApplicationContext() didn't work for me Commented Sep 7, 2016 at 8:23
  • You open NewActivity in MainActivity?
    – Katya
    Commented Sep 7, 2016 at 8:25
  • I'm sure I didn't call finish() on first activity Commented Sep 7, 2016 at 8:25

2 Answers 2

2

if I understood correctly you want to navigate to previously loaded webpages in your WebView rather than closing it from current page.

if that's true try this solution.

@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        super.onBackPressed(); // or finish();  
    }
}
3
  • No I don't, I want to navigate to previous activity but what happens actually is that pressing back button close all previous activities and return to MainActivity Commented Sep 7, 2016 at 8:20
  • from which activity are you calling webview? Commented Sep 7, 2016 at 8:21
  • I'm calling WebView inside NewActivity Commented Sep 7, 2016 at 8:24
0

Try to exit view.

@Override
 public void onBackPressed() {
 System.exit(REQUEST_CODE);
 super.onBackPressed();
}
1
  • Same problem and same behavior Commented Sep 7, 2016 at 8:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.