From my application I use an Intent to open a web page in the Browser. The default behaviour is that back button leads to previous page in the browser and not back to my application. Is there a way to force the back button to return the user to my App?
Asked
Active
Viewed 1,359 times
2
2 Answers
5
Once you use an Intent to open into another app, it's up to that App to handle the back button. If you just want to show a webpage as an activity, you can run your own activity that hosts a WebView
and use the following to open your web page:
webview.loadUrl("http://lolcats.com/");
And in your activity you can override the back button to do what you need to do:
@Override
public void onBackPressed()
{
// put code here to do things
}

jakebasile
- 8,084
- 3
- 28
- 34
-
Thank you. If I understand you right, there is no need to override something. WebView is just a view and back button works as expected - going to previous activity and not to previous page. Am I right? – Konstantin Milyutin Mar 08 '11 at 23:27
-
Yeah, you're right. I was thinking you may need to do something else fancy, but if all you need is to show a webpage and go back to previous activity on back button, you don't need to override. – jakebasile Mar 08 '11 at 23:34
-
May be you know answer to another question. Browser has the ability to launch another activities if it meet URI with appropriate schema. Is it possible with WebView? – Konstantin Milyutin Mar 08 '11 at 23:55
-
That should work, if the intended activity is declared with the apropriate intent filter. – jakebasile Mar 08 '11 at 23:56
-
I experienced frustrating behaviour. My application look like: Main -> Browser -> Main. If I leave Main as standard, then firstly browser creates new Main and secondly, if user goes Home he return to first Main, not second. Otherwise, if I make Main as singleTask, then browser finds first Main and all is fine. BUT, every going Home destroys the stack! All ways are bad... – Konstantin Milyutin Mar 09 '11 at 00:01
-
@Jake Basile But it's set by default. If I decide to use browser, can I tell it to clear the history of pages? Then back button will go back to my app as expected. – Konstantin Milyutin Mar 09 '11 at 12:33
-
I'm not aware of any way to tell that to the browser, and if there were it would only work for one specific browser, not taking into consideration if the user has chosen a different browser such as Opera or Skyfire. I think running your own activity with a webview is going to be your best bet. – jakebasile Mar 09 '11 at 16:43
-
Yes, I've implemented needed funtionality and it works smooth. I don't like browser forcing to start new task, but understand why they did it this way. – Konstantin Milyutin Mar 09 '11 at 17:18
1
How are you calling the browser? If you do it like this:
Uri url = Uri.parse("http://mysite.com/");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, url);
startActivity(launchBrowser);
then it ought to work as you expect.

RichieHindle
- 272,464
- 47
- 358
- 399
-
Yes, I'm doing it the same way. But I read that browser reimplement back button and does it different way. Can you explain? – Konstantin Milyutin Mar 08 '11 at 23:24