V
V
Vladislav2015-11-04 12:50:18
Android
Vladislav, 2015-11-04 12:50:18

How to save the state of a WebView that is in a dynamic fragment?

The problem is this, there is a dynamic fragment in which the WebView is located. When the device is rotated, the state of the WebView is not saved. How to save state of WebView?
This is in short, in more detail here is the code:
MainActivity:

package com.example.mrbik_000.myapplication;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;



public class MainActivity extends AppCompatActivity implements FInterface {
    private WebView webView;
    private MyFragment myFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState != null) {
            myFragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.container);
            myFragment.setFragmentListener(this);
        } else {
            myFragment = new MyFragment();
            myFragment.setFragmentListener(this);
            getSupportFragmentManager().beginTransaction().add(R.id.container, myFragment).commit();
        }
    }

    public void fragmentInitialized() {
        if (webView == null) {
            webView = (WebView) findViewById(R.id.webView);
            webView.setWebViewClient(new WebViewClient());
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl("http://stackoverflow.com/");
        }
    }
}

Fragment:
package com.example.mrbik_000.myapplication;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class MyFragment extends Fragment {
    private FInterface listener;
    private View view;

    public void setFragmentListener(FInterface listener) {
        this.listener = listener;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listener.fragmentInitialized();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment, container, false);
        return view;
    }
}

interface:
package com.example.mrbik_000.myapplication;

import android.os.Bundle;


public interface FInterface {
    void fragmentInitialized();
}

MainActivity implements the FInterface interface so that the WebView object can be obtained from this activity.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rustem Saitkulov, 2015-11-05
@atetc

Let the WebView live in the fragment, then there will be no change when rotating, but you will have to rewrite the logic. There are no easy ways to save the state of a WebView.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question