Browser does not reload the last page

1

My application is a simple web browser unique to my site, however I am facing a "problem" that is annoying.

This is the following, I am using the application well, I leave it in the background and open another browser for example google chrome or any other application that consumes a lot of memory and my app in the background it stops. When I go back to it, the page reload BUT instead of RECHARGING THE LAST PAGE THAT WAS ACCESSED, it reloads is the main link of the site ie the 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 get the above code so I can reload the last page?

    
asked by anonymous 05.09.2016 / 00:59

1 answer

2

Save the URL of the page you were viewing at the time the application passed the background.
You can use SharedPreferences to do this.

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%

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

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

In the onStop() method, set it to URL saved 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 the application to be abandoned by the user through the "Back Key" , the application starts up on the default page, change the method onCreate() as well :

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

    if(isFinishing()){
        saveUrl("http://xxxx.xx");
    }else{
        String url = mWebView.getUrl();
        saveUrl(url);
    }
}
    
05.09.2016 / 11:32