Dee Tee
Dee Tee

Reputation: 63

back button on webview exits app instead of displaying previous listview results

I am developing an application which initially loads a listview of items within onPostExecute. There was no problem opening fuller details on webview (still within the onPostExeceute) after item is selected. However, hitting the goback button within the webview just exits the whole application.

Can someone please help.

The scheme of events is thus Activity Create establish network broadcast --> onStart --> AsyncTask --> retrieves data --> onPostExecute --> setContentView (ListView) --> onClickListener --> WebView --> goback button at this stage then exits the entire application.

I'm sure someone can help me on this and I would really appreciate any effort.

Regards

Upvotes: 0

Views: 481

Answers (2)

Ashl
Ashl

Reputation: 1259

The default behavior of the back key in Android is to return the previous Activity, or if you are already at the root Activity, to exit the app. It sounds like your WebView is located within the same Activity as your ListView, so when you are clicking the "Back" key the app is exiting because there is no previous Activity to return to.

One solution would be to create a new Activity for displaying the WebView and pass in the data that you want to be displayed. With the WebView being loaded in a separate Activity, pressing the "Back" button will finish the WebView Activity bring you back to the list. You also get the added benefit of having screen transitions this way.

Another solution is to override the onKeyUp method in the ListView Activity and handle the pressing of the back key manually. (I personally recommend onKeyUp instead of onKeyDown because it makes it easier to handle long press that way) Capture the event and just undo whatever you are doing when an item in the list is clicked.

Upvotes: 0

Festus Tamakloe
Festus Tamakloe

Reputation: 11310

This behavior is normal. onBackPressed() event call finish() method in your activity and throw you back to where you just start from.

You still have the possibility to catch this event and tell your app what to do at next

Upvotes: 1

Related Questions