0

Hello I've a problem when using my app on the phone, when I rotate the screen inside the app the webview reloads all content getting back to the first page we were opening the app.

I leave you a gif of what happens here and if you know a solution please help me. I also leave my main class and the android manifest.

Proof Video

Main.java

AndroidManifest.xml

RavenZ
  • 1
  • 1

2 Answers2

0

Add android:configChanges="orientation|screenSize" in your Manifest.xml file

Like below:

<activity
        android:name=".MyActivity"
        android:configChanges="orientation|screenSize"
        android:label="@string/title_activity"
        android:theme="@style/AppTheme.MyTheme">

</activity>
yatin deokar
  • 730
  • 11
  • 20
0

Create a fragment to wrap your WebView up. Always return the same WebView in onCreateView, so that a new WebView would not be created on orientation change, thus not reloading.

public class MyWebViewFragment extends Fragment {
    private WebView mWebView;

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

    @Override
    public void onResume() {
        super.onResume();
        mWebView.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mWebView == null)
            mWebView = new WebView(getActivity());

        ViewGroup parent = (ViewGroup) mWebView.getParent();
        if (parent != null)
            parent.removeView(mWebView);

        return mWebView;
    }
}

Add this fragment to your activity.

CJL
  • 1