Is it possible to prevent a webView from refreshing/reloading when we go to another activity and come back to the same activity?

十年热恋 提交于 2020-02-06 08:09:16

问题


  1. Activity A = webView which contains smartchatbot(used for answering simple questions) url.

My question is very simple. All i want to achieve is when i go into Activity A and use the smartchatbot. Once i ask few questions to the smartchatbot, and the smartchatbot gives me a link to go to other page/activity. Once i view the page and return back to Activity A which contains the smartchatbot, i want all the previous questions i asked to the smartchatbot to be there. Not only that, even if i click back button the previously questions have to be there. I have tried the movetasktoback() method but when i click back in Activity A, the back button acts like a home button and goes to the main home screen of the phone.

How is it possible to achieve this?

I have tried to save the webView state in Acivity A by overriding the onSaveInstanceState method. However, this onSaveInstanceState method was never called. Im not sure whether this method is the suitable solution for what i want to achieve. Please help me. Thanks in advance


    private WebView webView;

    @Override
    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        webView.saveState(outState);
    }

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

        if(savedInstanceState != null){
            webView.restoreState(savedInstanceState);
        }else{
            setupWebViewUI();
        }

    }

               private void setupWebViewUI() {
                    webView.loadUrl("contains the smartchatbotURL");
                    WebSettings webSettings = webView.getSettings();
                    webSettings.setJavaScriptEnabled(true);
                }
            }

来源:https://stackoverflow.com/questions/59353675/is-it-possible-to-prevent-a-webview-from-refreshing-reloading-when-we-go-to-anot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!