-1

I have an android webview app, with more than 3 different activity my problem is when i lunch new activity and close it using finish(), the main webview activity will refresh again. Is it possible to prevent it from refreshing when i don't want it to and refresh only on the activity i want it to? I have follow few post but am getting errors on cannot resolve symbol FragmentIdentifier and other ones. Here is the link Prevent Reload Activity / Webview when go Back to app and when press the turn off button

Below is what i have in onresume method to remember last saved url.

protected void onResume() {
    super.onResume();
    //String message = getIntent().getStringExtra(FSMainActivity.FragmentIdentifier);
    if(FSwebview != null) {
        SharedPreferences r_prefs = getSharedPreferences(getPackageName(), MODE_PRIVATE);
        // String ssn = r_prefs.getString("sessionParameter","");
        String lastPage = r_prefs.getString("lastUrl", "");
        if(!lastPage.equals("")) {
            /*if (message == null || message.compareTo(FSMainActivity.showWebViewFragment) == 0) {
                if (!getSavedInstance()){
                    changeFragment(new WebViewFragment());
                }

            } else if (message.compareTo(FSMainActivity.showLoginFragment) == 0) {
                changeFragment(new LoginFragment());
        }*/
           appLoadUrl(lastPage, false);
       }else{
          appLoadUrl(APP_URL, false);
       }
    }
}

My webview app

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    WebView mWebView = new WebView(this);

    //now do whatever u want
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new HelloWebViewClient());
    mWebView.loadUrl("https://example.com/index2.php");
    mWebView.setInitialScale(130);

    //finally
    setContentView(mWebView);
}

I store last url below

 protected void onPause() {
        super.onPause();
        SharedPreferences prefs = getSharedPreferences(getPackageName(), MODE_PRIVATE);
        SharedPreferences.Editor edit = prefs.edit();
        edit.putString("lastUrl", FSwebview.getUrl());
        edit.commit();
    }
Peter
  • 1,860
  • 2
  • 18
  • 47
  • 1
    you can load your url in activity `onCreate()` method instead of `onResume()` – Vivek Mishra Oct 15 '18 at 04:33
  • @VivekMishra yes, my url is loaded in `onCreate()` them when app is closed entirely and open again it will load last url from `onResume()` – Peter Oct 15 '18 at 04:35
  • But since you are loading it in onResume() too, it will be getting called when your other activity closes and the webview activity comes in focus. – Vivek Mishra Oct 15 '18 at 04:37
  • So you have to keep it in `onCreate()` only. Or keep some boolean value in shared preference or static variable and chage it's value when you finish your other activity and in onResume() check for that value and handle your url loading – Vivek Mishra Oct 15 '18 at 04:39
  • @VivekMishra can you post an example answer? i have just updated my question – Peter Oct 15 '18 at 04:43
  • what do you meant by last URL ? where are you storing last URL in shared prefrences ? – Ali Ahmed Oct 15 '18 at 05:00
  • you have multiple URLs you want to run in a single webview ? – Ali Ahmed Oct 15 '18 at 05:01
  • @AliAhmed i have updated my question, i mean last url like `example.com/index1.php`, `example.com/index2.php`, `example.com/index1.php?get=example` – Peter Oct 15 '18 at 05:04
  • @Peter posted an answer. Please check – Vivek Mishra Oct 15 '18 at 05:08
  • @Peter .. Vivek has posted answer and he is right. you have two choices. you can follow his answer. – Ali Ahmed Oct 15 '18 at 05:17

1 Answers1

1

Since you are loading your urls in onResume() too, it will be getting called when your other activity closes and the webview activity comes in focus. Here you have 2 choices to go for.

  1. Only load your urls in onCreate() method as it will not get called again until your activity is restarted.
  2. Before finishing your second activity save some boolean value either in shared preferences or as a static variable for eg.

    sharedPreferences.putBoolean("activity finished",true);

And then in your onResume() method check for the above boolean variable value before loading your urls. Remember to set the value back to false according to your needs, preferably in your first activity onStop() method

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84