Android:保存XWalkView - Crosswalk状态

时间:2016-09-18 10:21:14

标签: android webview state crosswalk

我正在使用XWalkView将移动网站显示为应用程序。我的问题是当应用程序进入后台并返回时它重新加载它显示的页面。我希望保持状态并从背景中继续该状态。这是我的代码:

public class MainActivity extends AppCompatActivity {

    static final String URL = "https://www.biletdukkani.com.tr";
    static final int MY_PERMISSIONS_REQUEST_ACCESS_LOCATION = 55;
    static final String SHOULD_ASK_FOR_LOCATION_PERMISSION = "shouldAskForLocationPermission";
    static final String TAG = "MainActivity";
    static final String COMMAND = "/system/bin/ping -c 1 185.22.184.184";
    static XWalkView xWalkWebView;
    TextView noInternet;
    static Bundle stateBundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");

        // Check whether we're recreating a previously destroyed instance
        if (savedInstanceState != null) {
            // Restore value of members from saved state
            stateBundle = savedInstanceState.getBundle("xwalk");
        }

        setContentView(R.layout.activity_main);
        initNoInternetTextView();
    }


    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Always call the superclass so it can restore the view hierarchy
        super.onRestoreInstanceState(savedInstanceState);
        stateBundle = savedInstanceState.getBundle("xwalk");
        Log.d(TAG, "onRestoreInstanceState");
    }


    /**
     * İnternet yok mesajı gösteren TextVidew'i ayarlar.
     */
    private void initNoInternetTextView() {
        Log.d(TAG, "initNoInternetTextView");
        noInternet = (TextView) findViewById(R.id.no_internet);
        if (noInternet != null) {
            noInternet.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    checkInternetConnection();
                }
            });
        }
    }


    /**
     * WebView'i ayarlar.
     */
    private void initWebView() {
        Log.d(TAG, "initWebView");
        if (xWalkWebView == null) {
            ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
            xWalkWebView = (XWalkView) findViewById(R.id.webView);
            //xWalkWebView.clearCache(true);
            xWalkWebView.load(URL, null);
            xWalkWebView.setResourceClient(new BDResourceClient(xWalkWebView, progressBar));
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
        checkLocationPermissions();
        checkInternetConnection();

        if (xWalkWebView != null && stateBundle != null) {
            xWalkWebView.restoreState(stateBundle);
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
        if (xWalkWebView != null) {
            stateBundle = new Bundle();
            xWalkWebView.saveState(stateBundle);
        }
    }


    public void onSaveInstanceState(Bundle savedInstanceState) {
        Log.d(TAG, "onSaveInstanceState");
        // Save the user's current game state
        savedInstanceState.putBundle("xwalk", stateBundle);
        // Always call the superclass so it can save the view hierarchy state
        super.onSaveInstanceState(savedInstanceState);
    }


    @Override
    public void onBackPressed() {
        Log.d(TAG, "onBackPressed");
        if (xWalkWebView != null && xWalkWebView.getNavigationHistory().canGoBack()) {
            xWalkWebView.getNavigationHistory().navigate(XWalkNavigationHistory.Direction.BACKWARD, 1);
        } else {
            super.onBackPressed();
        }
    }
}

我还尝试在清单中添加以下行但不起作用。

android:launchMode="singleTask"
android:alwaysRetainTaskState="true"

我该怎么做? 先谢谢。

1 个答案:

答案 0 :(得分:0)

一种方法是初始化片段内的视图,该片段设置为保留其实例。