0

I'm having an issue with my code and I'm not sure if the way I'm writing my program is the best way to write it.

To cut to the chase..

in the onCreate method of my application, setContentView(R.layout.activity_main); is called.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//extra code..
//there is a lot of code from here on out, but it is not relevant to my question.
}

A WebView is displayed with 4 images loaded onto the webview. The user can click either one of the images and it will take the user to 1 of 4 different websites, with each image corresponding to a different url.

In this application, I am overriding public void onBackPressed()

Now here is the problem.

When the user broweses the webpage and has a "history"(all of this is handled in the webview), meaning the user can click back, it will keep going back to the beginning of the page, right to where the user started when they clicked one of the images.. But it does NOT go back to the view from where the program originally starts.. meaning that when the user is browsing a page in my webview and they click the back button , the furthest it goes back to is the start of the webpage.

What I want it to go back to is to the beginning of the app, where the 4 images appear on screen and the user can click a different image, (i.e. a different webpage to select).

What I've tried To solve this probelm, I assumed I would try and create a condition. If the webpage cannot go back any further, a method would call setContentView(R.layout.activity_main) again.

This DOES work.. with only one big problem. When the layout loads, all of the 4 images do not respond to any sort of clicks, and the application will not exit even with the press of the back button.

No exceptions are thrown, no crashes occur.

What would be a good solution to my problem?

And if needed, I can post code.

Dean Z
  • 57
  • 1
  • 9
  • images will receive click if u define onClick in xml not in onCreate, i havn't clearely got ur prob but i must say this is not a good sol – Manmohan Badaya Jan 24 '14 at 19:39
  • http://stackoverflow.com/questions/6077141/android-webview-how-to-code-the-back-button – qwr Jan 24 '14 at 19:42
  • Did you try webview.loadUrl() again or you have a particular reason to not do that? – Orhan Obut Jan 24 '14 at 19:48
  • is your webview and imagevies in the same activity? inside overrided OnBakcPressed clear Webview (or even set to invisible) and images to visible – qwr Jan 24 '14 at 19:51
  • Yeah they're in the same activity. and nr4bt, I did not try that because that would load the webpages. I want to load the 4 images that LEAD to the webpages when the images are clicked. – Dean Z Jan 24 '14 at 19:52
  • Ah images inside webview.well just load webview again like you do it oncreate. – qwr Jan 24 '14 at 19:53
  • how would I load the webview again? – Dean Z Jan 24 '14 at 19:54
  • ((WebView)findViewById(R.id.yourwebviewId)).loadUrl("your html url"); or ((WebView)findViewById(R.id.yourwebviewId)).loadData(yourhtmlstring, "text/html", "UTF-8"); or better first get WebView web=(WebView)findViewById(R.id.yourwebviewId); then check and cal if(web!=null) web.LoadUrl(..) – qwr Jan 24 '14 at 19:56
  • Sigh..... I did that..and it works. I feel incredibly dumb now.. I haven't felt this down in a while. Thank you... can you post your answer so I can up vote and accept you? – Dean Z Jan 24 '14 at 20:03
  • check also link that I provide where is simple way to make webbased App (in which there is shown how to create , override OnBackPressed and so on) Also for more always just check android docs.There you can find all methods and related classes to webview – qwr Jan 24 '14 at 20:40

2 Answers2

1
  ((WebView)findViewById(R.id.yourwebviewId)).loadUrl(Url); 
//or
 ((WebView)findViewById(R.id.yourwebviewId)).loadData(yourhtmlstring, "text/html", "UTF-8"); 

//or safer 
 WebView web=(WebView)findViewById(R.id.yourwebviewId);
 if(web!=null) web.LoadUrl(..) 

More on webview methods just check android documentation

Assuming you want just to create webviewapp. Here is good tutorial WebView-based Applications

qwr
  • 3,660
  • 17
  • 29
0

The problem is that you are loading the main activity's layout into the webview.

What you want to do is to navigate back to the main activity.

Simply calling finish() instead of setContentView() should do the trick.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • I called `finish()` and what that does is it exits the app. I want to go back into the beginning of the app instead of exiting it. – Dean Z Jan 24 '14 at 19:45