Android如何在屏幕关闭/打开时阻止webview重新加载?

时间:2017-10-09 04:44:27

标签: android android-activity android-webview android-lifecycle application-state

我看到很多与我的问题相关的文档和文章。但我没有得到任何适当的解决方案。当我按下电源按钮打开/关闭时,WebView将被重新加载。我不想重新加载WebView

我在想什么......

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;
}} 

我正在尽我所能。我还在 AndroidManifest.xml 文件中添加了android:launchMode="singleTask"。你能为我的问题提供任何解决方案。

3 个答案:

答案 0 :(得分:1)

尝试

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

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

而不是

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

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

答案 1 :(得分:0)

尝试:

if (savedInstanceState == null)
{
web.loadUrl(webURL);
}

答案 2 :(得分:0)

尝试添加生命周期方法:

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

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
web.restoreState(savedInstanceState);
}