I have read all the topics concerning this, but I have not been able to solve the problem.
I have a webview for each tab, I change the tabs from the navigation bottom menu. I want to block the webview refresh when I change tabs
I tried to use:
if (webViewBundle != null)
{
mWebView.restoreState(webViewBundle);
}
...
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
mWebView.saveState(webViewBundle);
}
Or
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if (webViewBundle != null)
{
mWebView.restoreState(webViewBundle);
}
}
And
if (savedInstanceState != null) {
mWebView.restoreState(savedInstanceState);
}
...
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
mWebView.saveState(savedInstanceState);
}
Nothing seems to work, every time I change the fragment the webview is reloaded.
This is the simplified MainActivity:
public class MainActivity extends AppCompatActivity {
private BottomNavigationView navView;
private Context context;
private NavController navController;
private AppBarConfiguration appBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
navView = findViewById(R.id.nav_view);
appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_dashboard, R.id.navigation_search, R.id.navigation_tip,R.id.navigation_notifications, R.id.navigation_profile).build();
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
}
This is one of the 5 fragments:
public class DashboardFragment extends Fragment {
private Context context;
private WebView mWebView;
@SuppressLint({"JavascriptInterface", "ClickableViewAccessibility"})
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
mWebView = root.findViewById(R.id.home_webview);
if (savedInstanceState != null) {
mWebView.restoreState(savedInstanceState);
}else{
mWebView.loadUrl(URL);
}
return root;
}
public void onSaveInstanceState(Bundle outState) {
mWebView.saveState(outState);
super.onSaveInstanceState(outState);
}
}