且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

返回应用程序并按关闭按钮时,防止重新加载活动/Webview

更新时间:2023-02-16 22:32:51

我发现我应该处理该实例并检查是否已保存,否则我的onResume总是可以重新加载我的Web视图.这是我的新代码:

I figured out that I should handle the Instance and check if already saved or not, otherwise my onResume could always reload my webview. Here my new code:

@Override
protected void onResume() {
    super.onResume();

    //Check if the user is authenticated onResume
    String message = getIntent().getStringExtra(MainActivity.FragmentIdentifier);

    if (message == null || message.compareTo(MainActivity.showWebViewFragment) == 0) {

        /*
         Check if the instance is saved. If yes, is not necessary to create a new instance of the webview,
         otherwise will always reload.
        */
        if (!getSavedInstance()){
            changeFragment(new WebViewFragment());
        }

    } else if (message.compareTo(MainActivity.showLoginFragment) == 0) {
        changeFragment(new LoginFragment());
    }

}

我声明了一个新的全局变量

I declared a new global variable

private Boolean savedInstance = false;

在这里,我得到并设置

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    setSavedInstance(true);
}

public Boolean getSavedInstance() {
    return savedInstance;
}

public void setSavedInstance(Boolean instance_saved) {
    this.savedInstance = instance_saved;
}