How to open Webview in the last URL accessed

Asked

Viewed 71 times

0

I’m making a web app with just one nick login.

Every time someone opens the app, they only enter the variable url where the page asks for the nickname, but I wanted it to save the last URL to access it the next time the user opens the app, and if there is no saved URL, it opens the default URL. So the user doesn’t have to type his nickname every time.

This is my current code:

public class MainActivity extends AppCompatActivity { 
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide(); //Escconde TopBar
        setContentView(R.layout.activity_main);

        //Declanação variavel url
        String url = "http://endereçodomeusiteapp";
        String eror = "Sem erro";


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

        //Habilita javaScript
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        //Url Padrão.
        webView.loadUrl(url);


        webView.setWebViewClient(new WebViewClient(){
                public void onReceivedError(WebView view, int eror, String description, String html) {
                    webView.loadUrl("file:///android_asset/conex.html");

        }});

1 answer

0


Hi there, Leonidas!

Since you say login is just a nickname, I will consider that the security level should not be a problem. That way, we can solve your problem as follows

1 - Create a method to save your last url

public void salvaUrl(String url, SharedPreferences.Editor editor) {
   editor.putInt(EXTRAS_ULTIMA_URL, url);
   editor.apply();
}

2 - Create another method to recover

public String recuperaUrl(String nomePref, SharedPreferences prefs) {
   // Esse null é um valor padrão caso não exista valor (url) salvo
   return prefs.getString(nomePref, null);
}

3 - In the method onStop of Activity save the last url using step 2 method

@Override
protected void onStop() {
   super.onStop();
   if (webView != null && mPrefs != null) {
      salvaUrl(webView.getUrl(), mEditor);
   }
}

4 - Now let’s put it all together

public class MainActivity extends AppCompatActivity {
    private final static String EXTRAS_ULTIMA_URL = "extras_ultima_url"; 
    private final static String NOME_PREFS        = "nome_prefs";

    private WebView webView;
    private SharedPreferences mPrefs;
    private SharedPreferences.Editor mEditor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide(); //Escconde TopBar
        setContentView(R.layout.activity_main);

        mPrefs  = getSharedPreferences(NOME_PREFS, Context.MODE_PRIVATE);
        mEditor = prefs.edit();

        //Declanação variavel url
        String url = "http://endereçodomeusiteapp";
        String eror = "Sem erro";


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

        //Habilita javaScript
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // Antes de chamar sua url de login, verifique se há uma última 
        // url salva utilizando o método criado no passo 1
        String ultimaUrl = recuperaUrl(EXTRAS_ULTIMA_URL, mPrefs);

        if (ultimaUtl != null) {
           webView.loadUrl(utlimaUrl);
        }
        else {
           webView.loadUrl(url);
        }

        webView.setWebViewClient(new WebViewClient(){
                public void onReceivedError(WebView view, int eror, String description, String html) {
                    webView.loadUrl("file:///android_asset/conex.html");

        }});
     }

     public void salvaUrl(String url, SharedPreferences.Editor editor) {
          editor.putInt(EXTRAS_ULTIMA_URL, url);
          editor.apply();
     }

     public String recuperaUrl(String nomePref, SharedPreferences prefs) {
         // Esse null é um valor padrão caso não exista valor (url) salvo
         return prefs.getString(nomePref, null);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (webView != null && mPrefs != null) {
            salvaUrl(webView.getUrl(), mEditor);
        }
    }
}
  • Thank you very much friend, God bless you. It worked perfectly, I am very grateful for your help.

  • I am very grateful to see that I was able to help you. Hug!

Browser other questions tagged

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