3

I saw a lot of documentation and articles related to my questions.But I don't get any proper solutions. When I press the power button turn On/Off, The WebView would be reloaded.I don't want to reload the WebView.

What am I trying is....

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    StrictMode.ThreadPolicy policy = new 
    StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    loadpages();
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (!ConnectivityStatus.isConnected(context)) {
            // no connection
            WebView wv = (WebView) findViewById(R.id.webView1);
            wv.loadData(iError, "text/html", "UTF-8");
            // Toast.makeText(getApplicationContext(),"Internet connection failure",Toast.LENGTH_SHORT).show();
        }
    }
};

@Override
protected void onResume() {
    super.onResume();
    this.registerReceiver(receiver, new 
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
 @Override
protected void onPause() {
    super.onPause();
    this.unregisterReceiver(receiver);
 }
@Override
public void onBackPressed() {
    AlertDialog.Builder alertDialog = new 
   AlertDialog.Builder(MainActivity.this);
    alertDialog.setTitle("Local");
    alertDialog.setMessage("Exit Application");
    alertDialog.setCancelable(true);
    alertDialog.setPositiveButton("YES", new 
    DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
       // User pressed YES button. Write Logic Here
            MainActivity.this.finish();  
        }
    });
    alertDialog.setNegativeButton("NO", new 
   DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // User pressed No button. Write Logic Here
            //   Toast.makeText(getApplicationContext(), "You clicked on 
           NO", Toast.LENGTH_SHORT).show();
        }
    });
    alertDialog.show();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}
@SuppressLint("NewApi")
public boolean loadpages() {
    WebView wv = (WebView) findViewById(R.id.webView1);
    WebSettings webSettings = wv.getSettings();
    webSettings.setJavaScriptEnabled(true);
    wv.getSettings().setLoadWithOverviewMode(true);
    wv.getSettings().setUseWideViewPort(true);
    CookieManager.getInstance().setAcceptCookie(true);
     TelephonyManager mngr = (TelephonyManager) 
    getSystemService(Context.TELEPHONY_SERVICE);
    String imeino = mngr.getDeviceId().toString();
   CookieManager.getInstance().setCookie
   ("http://MyLocalHost/projects/aaaaa", "IMEINO=" + imeino);
    String url = "http://MyLocalHost/projects/aaaaa/index.php"
    wv.setWebViewClient(new WebViewClient() {
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (!loadingFinished) {
    redirect = true;
    }
    loadingFinished = false;
    view.loadUrl(url);
    return true;
    }
  @SuppressWarnings("unused")
  public void onPageStarted(WebView view, String url) {
    loadingFinished = false;
    // SHOW LOADING IF IT ISNT ALREADY VISIBLE
    }
  public void onPageFinished(WebView view, String url) {
    if (!redirect) {
    loadingFinished = true;
    }
    if (loadingFinished && !redirect) {
    // HIDE LOADING IT HAS FINISHED
    } else {
    redirect = false;
     }
    }
    });
    loadingFinished = false;
    wv.loadUrl(url);
   return loadingFinished;
}} 

I'm trying to my best. I've also added android:launchMode="singleTask" in AndroidManifest.xml file. Can you guys give any solution for my problem.

karthi
  • 183
  • 1
  • 3
  • 14
  • Check your broadcast receiver onReceive(). If you are receiving onResume(), your webview will load whenever fragment/activity is resumed. – james Oct 09 '17 at 06:12
  • @james yup i checked.but not get any solution – karthi Oct 09 '17 at 09:21

3 Answers3

1

Try

@Override
protected void onStart() {
    super.onStart();
    this.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}

@Override
protected void onStop() {
    super.onStop();
    this.unregisterReceiver(receiver);
}

instead of

@Override
protected void onResume() {
    super.onResume();
    this.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
} 

@Override
protected void onPause() {
    super.onPause();
    this.unregisterReceiver(receiver);
}
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
0

try:

if (savedInstanceState == null)
{
web.loadUrl(webURL);
}
sourabh kaushik
  • 523
  • 4
  • 20
  • in on resume and on pause it just check the network connectivity – karthi Oct 09 '17 at 06:03
  • then tell me your mobile's memory is full or you have some space if yes then tell me the remaining space – sourabh kaushik Oct 09 '17 at 06:11
  • If you put your code in onCreate() method whenever the Activity goes to stop state and if the device memory is less then the process is killed and memory is allocated to the high priority app. while coming back it resumes from the oncreate() method, here not only your webView but entire app is reloaded. – sourabh kaushik Oct 09 '17 at 06:13
0

try adding in your lifecycle method:

@Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
web.saveState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
web.restoreState(savedInstanceState);
}
A2N
  • 150
  • 2
  • 13
  • check from where your webview is reloading after screening on., i mean put log in oncreate,onpause,onresume,loadpages method – A2N Oct 09 '17 at 05:51
  • @ A2N on screen lock app calls onpause or onStop.screen unlocks app calls on Resume. – karthi Oct 09 '17 at 05:54
  • set up a webviewclient and js enabled as true – A2N Oct 09 '17 at 06:52
  • 1. start a timer at the same time you started loading the webview. 2.inside the timer check for isloadingFinished and show the error screen if needed https://developer.android.com/reference/android/os/CountDownTimer.html – A2N Oct 10 '17 at 05:32