I ask if it is possible to solve the following:
- keep the content of a webview contained in a fragment when changing to another fragment
What I have implemented and copied below, resets the content every time the snippet is displayed. For example: if you are completing a form, you go back to home (google.com) and this is lost.
public class GalleryFragment extends Fragment {
WebView miVisorWeb;
String url = "https://www.google.com/";
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_gallery, container,false);
// Bloque 1
miVisorWeb = (WebView) root.findViewById(R.id.webViewGoogle);
miVisorWeb.requestFocus();
WebSettings webSettings = miVisorWeb.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setCacheMode(LOAD_CACHE_ELSE_NETWORK);
webSettings.setAppCacheEnabled(true);
miVisorWeb.loadUrl(url.toString());
CookieManager.getInstance().setAcceptCookie(true);
// Bloque 2
miVisorWeb.setWebViewClient( new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
miVisorWeb.setOnKeyListener(new View.OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
WebView webView = (WebView) v;
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack())
{
webView.goBack();
return true;
}
break;
}
}
return false;
}
});
return root;
}
}
Suppose a video is played in fragment A, when I change to B and return to A the idea is that the playback continues.
Similar to browser tabs, the idea would be that.
Cheers!!!
What you can try is the following:
Remembering a bit the life cycle of the fragment, under the idea that you want to realize, you can move the code that you have the
onCreate()
. Remember that this method allows you to initialize some things and when you return to this fragment you will not re-execute this method if and only if it was already created. With the idea that you have as your fragment was already created, it will not be re-executed (restarted). For now you have it in the method .onCreateView()
I would recommend that you move it to theonCreate()
. Then when you go to call some fragment, remember that we can override ( methodreplace()
) of fragmentManager and we canagregar
( methodadd()
) of fragmentManager. For your case, what is best for you is to make aadd()
. I leave you an example:To add :
To replace :
update
Example :
Now when you want to call another fragment what you have to do is the following:
Remember that the parameter
R.id.fragment_container
is the id of your fragment container andfirstFragment
is the fragment you want to go to. Just replace those values with the ones you have in your projectThen, citing your example. If you go back from B to A, the code you have in A will no longer be executed. Because it's only in the method
onCreate
Another way to work around so you're not handling fragments is to do it with activities. Following the same example of fragments. In your activity, let's call it A. You add all the code you have. Then just to pass to an activity B. you call
startActivity
. When you do back, the code you have in the methodonCreate
of activity A will not be executed either.References: FragmentManager
I hope it helps you :)