Atul Goyal
Atul Goyal

Reputation: 3521

How to get control back to application (outside Webview)?

I am using Webview in an Android activity (say Activity B) like this

webview = new WebView(this);
webview.setWebViewClient(new WebViewClient());
setContentView(webview);
webview.loadUrl("http://mywebpage");

So, the webview opens a webpage in the activity, and the expected functionality is that user can browse different web pages following the links in the webview. (The website is of the mobile application, or in other words, is in my control).

Now, after the user is done checking out the webpages, how do I take the user to another activity? (if it helps the activity - activity A - will be the same from where the activity B containing the webview was called).

So can I, from the webview decide to finish the current activity (B) so the user is back to activity A? Or if possible, directly start another activity which could be activity A or some other activity C based on some event on the webpage?

Or will I need to provide a button outside the Webview on Activity B from which I can handle the flow?

Or there is a better approach?

Upvotes: 0

Views: 1081

Answers (2)

ootinii
ootinii

Reputation: 1815

There are several ways you can handle what you're describing. First of all, if the user hits their back button, the activity should finish just fine and return to activity A, without the need to call finish.

Now, if you are creating/in control of the webpages they are seeing, you can create intent filters for starting another activity from the web view. For instance:

<intent-filter>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="http" 
          android:host="www.yourwebsite.com" 
          android:path="your/path/here"/>
    <action android:name="android.intent.action.ACTION_VIEW"/>
</intent-filter>

If you apply that intent filter to an activity in your manifest, and you put a link on your website to the url http://www.yourwebsite.com/your/path/here, the activity supporting that intent filter should be launched.

Upvotes: 1

Theo
Theo

Reputation: 6083

What I have done in the past is provide a close button below the webview in the webview activity (in your case Activity B). If you invoke Activity B from Activity A using startActivityForResult, then the control will return to Activity A once Activity B closes (to be more specific, once Activity B closes, onActivityResult is called in Activity A).

In conjunction with the above, another thing you can try if you have a defined browsing process starting from page A and going to Page X is that you can auto-close Activity B when Page X is reached. Extend WebViewClient, listen to the onPageFinished event to get the current page URL and close the activity if the current URL matches the termination URL.

Upvotes: 1

Related Questions