安卓如何防止屏幕关闭/打开时重新加载网络视图



我看到了很多与我的问题相关的文档和文章。但我没有得到任何适当的解决方案。当我按下电源按钮打开/关闭时,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"。你们能为我的问题提供任何解决方案吗?

试试

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

尝试:

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

尝试在生命周期方法中添加:

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

最新更新