0

Folks I have an activity that launches an embedded browser view rendering stuff that is out of my control. Now suppose the user navigates one more level by clicking on a link in the browser view. This opens a new browser window. In order for the user to go back to prior activity, the user would have to click back "twice".

I would like to implement a "soft" back button that always shows up at the top of the embedded browser view.

I found the question answered at Android WebView, how to handle redirects in app instead of opening a browser

But when I use that solution, the android native button also takes you back directly to the prior activity (instead of going back in history of the page). Is there a way so that I can do both (have a custom back button that takes me back all the way but have the android native back retain the expected behavior of going back one page at a time?

Thank you!

Community
  • 1
  • 1
serverman
  • 1,314
  • 5
  • 22
  • 39

1 Answers1

5

I would like to implement a "soft" back button that always shows up at the top of the embedded browser view.

This will be pointless in your current implementation. As you state:

Now suppose the user navigates one more level by clicking on a link in the browser view. This opens a new browser window.

Note the key phrase here is "a new browser window", referring to the user's default choice of browser app. You cannot "implement a 'soft' back button that always shows up at the top of" the user's default choice of browser app. While you could put a Button alongside your WebView, that Button will only be visible when the activity containing the WebView is, and there, the user can press the ordinary hardware BACK button to exit that activity.

But when I use that solution, the android native button also takes you back directly to the prior activity (instead of going back in history of the page).

That is because there is only one activity, and the BACK button does not automagically have meaning for an activity with a WebView. For example, suppose the activity had two WebViews -- which one would BACK handle?

Is there a way so that I can do both (have a custom back button that takes me back all the way but have the android native back retain the expected behavior of going back one page at a time?

You can override onBackPressed() (or onKeyDown() for pre-Android 2.0) to get control when the hardware BACK key is pressed. You can use that to move back in the WebView history, or if there is no history, allow the normal processing (finish()) to occur.

I would recommend an action bar for allowing users to switch to some other mode of your app. Action bars are part of Honeycomb, and there are open source implementations you can use for earlier versions of Android.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanx CommonWare. I think you answered most of my question. I would mark this as the correct answer for now. – serverman Feb 10 '11 at 17:59