Android: save XWalkView - transition state

I am using XWalkView to display a mobile website as an application. My problem is when the application goes into the background and returns, it reloads the page that it shows. I want to save his state and continue from this state when it comes from the 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 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 also tried adding the following lines to the manifest, but didn't work.

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

How can i do this? Thanks for overcoming.

+6
source share
1 answer

One way is to initialize the view inside the fragment that is set to save the instance.

0
source

All Articles