Reopened applciation does not load url in webview

By | July 12, 2018
Questions:

I have a fragment with WebView inside to open url and login to some service and then receive some cookie. The issue is that when I open application and fragment inside activity, url loads into WebView normally, but when I close application (programatically with activity.finishAndRemoveTask()) and open it again (manually) – url does not load. After next app restart (manually) everything goes back to normal. Url which I want to open has two redirects.

It is my fragment class:

public class MyFragment extends Fragment implements WebResourceClient.CookieListener {

    private String URL = "https://someurl.com";

    @OnClick(R.id.close)
    public void detachFragment() {
        FragmentManager manager = getFragmentManager();
        if (manager != null) {
            manager.popBackStackImmediate();
        }
    }

    @BindView(R.id.web_container)
    WebView webContainer;

    @BindView(R.id.progress_bar)
    ProgressBar progressBar;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ny_fragment, container, false);
        ButterKnife.bind(this, view);
        startWebView();
        return view;
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void startWebView() {
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookies(null);

        WebSettings settings = webContainer.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        settings.setMediaPlaybackRequiresUserGesture(false);
        settings.setUserAgentString(Core.USER_AGENT_FOR_GOOGLE);
        settings.setAppCacheEnabled(false);

        webContainer.clearCache(true);
        WebView.setWebContentsDebuggingEnabled(false);
        webContainer.setWebViewClient(new WebResourceClient(this));
        webContainer.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress < 100) {
                    webContainer.setVisibility(View.INVISIBLE);
                    progressBar.setVisibility(View.VISIBLE);
                } else {
                    webContainer.setVisibility(View.VISIBLE);
                    progressBar.setVisibility(View.INVISIBLE);
                }
            }
        });

        webContainer.loadUrl(URL);
    }

    @Override
    public void onDestroyView() {
        webContainer.stopLoading();
        super.onDestroyView();
    }

    @Override
    public void onReceiveCookie(String cookie) {
        detachFragment();
        ...
    }

    @Override
    public void showProgress() {
        webContainer.setVisibility(View.INVISIBLE);
        progressBar.setVisibility(View.VISIBLE);
    }
}

It’s my WebViewClient:

public class WebResourceClient extends WebViewClient {


    private final static String someUrl = "https://someOtherUrl";

    private CookieListener listener;

    public WebResourceClient(CookieListener listener) {
        this.listener = listener;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        String url = request.getUrl().toString();
        if (url.contains(someUrl)) {
            listener.showProgress();
        }
        view.loadUrl(url);
        return true;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains(someUrl)) {
            listener.lockWebView();
        }
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
//        if (url.contains(someUrl)) {
        String cookies = CookieManager.getInstance().getCookie(url);
        String cookie = getCookie(cookies);
        if (!TextUtils.isEmpty(cookie)) {
            view.stopLoading();
            listener.onReceiveCookie(cookie);
        }
        super.onPageFinished(view, url);
    }

    private String getCookie(String cookies) {
       ...
    }

    public interface GoogleLoginListener {
        void onReceiveCookie(String cookie);

        void showProgress();
    }
}

I open fragment like there:

MyFragment fragment = new MyFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tr = fm.beginTransaction();
tr.addToBackStack(fragment.getClass().getSimpleName());
tr.replace(R.id.fragment_root, fragment);
tr.commit();
Answers:

Leave a Reply

Your email address will not be published. Required fields are marked *