1
My application is a simple web browser unique to my site, however I am facing a "problem" that is irritating.
It is the following, I am using the application in good ai I leave in the background and open another browser for example google Chrome or any other application that consumes a lot of memory and my application in the background it for. When I return to it, the page reloads BUT INSTEAD OF RELOADING THE LAST PAGE ACCESSED, it reloads is the main link of the site or index.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (isOnline()) {
Toast.makeText(getApplicationContext(), "Carregando", Toast.LENGTH_SHORT).show();
//mWebView = (WebView) findViewById(R.id.webview);
mWebView = (WebView) findViewById(R.id.webview);
Uri uri = Uri.parse("http://xxxx.xx");//Link por defeito
Intent intent = getIntent();
if(intent.getAction() == Intent.ACTION_VIEW){
uri = intent.getData();
}
mWebView.loadUrl(uri.toString());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(false);
mWebView.setWebViewClient(new LinkWebViewClient());
mWebView.requestFocusFromTouch();
mWebView.setWebChromeClient(new WebChromeClient());
}
else
[...]
}
private class LinkWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
if(isOnline()) {
Toast.makeText(getApplicationContext(), "Loading", Toast.LENGTH_SHORT).show();
webview.loadUrl(url);
return true;
}
else
{
Toast.makeText(getApplicationContext(), "Sem conexão", Toast.LENGTH_SHORT).show();
setContentView(R.layout.conexaofail);
return false;
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
if (isOnline()) {
mWebView.goBack();
return true;
}
else
{
setContentView(R.layout.conexaofail);
return false;
}
}
return super.onKeyDown(keyCode, event);
}
How do I use this code above to reload the last page?
Ramaral worked, just to remind that there is an error in your code: preferences.edit.putString("Url",url). apply(); where I switched to preferences.Edit(). putString("Url",url). apply();. Taking Advantage: Is there no way to stop the app from running in the background? and thank you so much for helping me!
– user41630
Jeez, a problem: When I close the application and open it again it opens in the last URL. Is there no way to delete the saved URL if you close the application? I wanted it only if it caught in the background.
– user41630
What do you mean by "close the app"? Android does not have this concept. Note that the default Android browser itself has this behavior. No, there is no way to prevent the application from being destroyed if Android needs memory.
– ramaral
Override the onDestroy method():
protected void onDestroy(){ super.onDestroy(); saveUrl("http://xxxx.xx");}
and pray that it is not called when the Android destroy the application, for lack of memory.– ramaral
Okay, I’ll test it! Thanks again!
– user41630
Hello @ramaral, could you explain a little about this "onBackPressed()"? type put here in my application and clicked on "back" button until exit dp App no longer saved the URL in saveUrl(). Can help me?
– user41630
The method
onBackPressed()
is called when the user clicks on the "back" button. I think what is happening is that the methodonStop()
Being called before the application finishes saves the URL that is in webview.– ramaral
I edited the answer. Instead of using
onBackPressed()
check in theonStop()
if the app is being closed.– ramaral
It worked as expected! Thank you very much!
– user41630
help me with that question: http://answall.com/questions/169537/posicionar-bot%C3%A3o-em-cima-do-an%C3%Bancio
– user41630