Browser does not reload last page

Asked

Viewed 81 times

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?

1 answer

1


Save the URL of the page that was being viewed at the time the application went into the background.
Can use the Sharedpreferences for that reason.

Write two methods, one to save and one to read:

private void saveUrl(String url){
    SharedPreferences preferences =  PreferenceManager.getDefaultSharedPreferences(this);
    preferences.edit().putString("Url",url).apply();
}

private String readUrl(){
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    return preferences.getString("Url","http://xxxx.xx");// se não houver nenhum guardado usa o Link por defeito
}

Use the method onStop() of Activity to guard the URL

@override
protected void onStop(){
    super.onStop();

    String url = mWebView.getUrl();
    saveUrl(url);
}

In the method onCreate(), law the URL kept and use it in Webview:

if (isOnline()) {
    Toast.makeText(getApplicationContext(), "Carregando", Toast.LENGTH_SHORT).show();

    //mWebView = (WebView) findViewById(R.id.webview);

    mWebView = (WebView) findViewById(R.id.webview);

    String url = readUrl();//Lê o url guardado
    Uri uri = Uri.parse(url);//usa o url 

    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{
.....
.....

If you want, when the application is abandoned by the user through the "Back Key", the startup application on the page default, change the method onStop() thus:

@override
protected void onStop(){
    super.onStop();

    if(isFinishing()){
        saveUrl("http://xxxx.xx");
    }else{
        String url = mWebView.getUrl();
        saveUrl(url);
    }
}
  • 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!

  • 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.

  • 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.

  • 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.

  • Okay, I’ll test it! Thanks again!

  • 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?

  • The method onBackPressed() is called when the user clicks on the "back" button. I think what is happening is that the method onStop() Being called before the application finishes saves the URL that is in webview.

  • I edited the answer. Instead of using onBackPressed() check in the onStop() if the app is being closed.

  • 1

    It worked as expected! Thank you very much!

  • help me with that question: http://answall.com/questions/169537/posicionar-bot%C3%A3o-em-cima-do-an%C3%Bancio

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.