6

I am using XWalkView to show a mobile web site as an application. My problem is when application goes background and comes back it reloads the page it shows. I want to keep it state and continue from that state when it comes from background. Here is my code:

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

I have also tried to add following lines to manifest but didn't work.

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

How can i do that? Thanks in advcance.

Ucdemir
  • 2,852
  • 2
  • 26
  • 44
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112

1 Answers1

0

One way would be to initialize the view inside a fragment which is set to retain it's instance.

shtolik
  • 1,341
  • 22
  • 29