简体   繁体   中英

Android Activity Lifecycle called twice when starting new Activity

I am basically starting new Activity which contains a WebView. But this WebView Activity's lifecycle called twice when first run. When destroy this activity and reopen again everything works fine.

Activity A runs

Act A -> onCreate

Act A -> onStart

Act A -> onResume

Start new activity with

startActivity(new Intent(this, WebViewActivity.class));

Activity B (WebView Activity) runs

Act A -> onPause

Act B -> onCreate

Act B -> onStart

Act B -> onResume

Log info may be cause this situation

W/WindowManager: Attempted to set replacing window on non-existing app token Token{1a4a5a ActivityRecord{219a05 u0 ui.activity.WebViewActivity t130}}

then continue with

Act B -> onPause

Act B -> onStop

Act B -> onDestroy

Act B -> onCreate

Act B -> onStart

Act B -> onResume

Act A -> onStop

I have tested this situation with many devices

Xiaomi mi a2 Lite Api 27-> error happens
Lg g4 Api 24 -> error happens
Huawei Nexus 6P Api 27-> error happens
Huawei Mate 10 Lite Api 26 -> error happens

Emulator Api 21 -> error not happens
Xiomi mi a2 Api 27 -> error not happens

is it a device bug? or OS bug? How can I fix it? if anyone can help, I will be appreciate.

My WebView Activity Code

public class WebViewActivity extends BaseActivity implements ViewClickHandler {

    public static final String TAG = WebViewActivity.class.getSimpleName();

    public static final String URL = "https://www.google.com";

    ActivityWebViewBinding binding;

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

        binding = DataBindingUtil.setContentView(this, R.layout.activity_web_view);
        binding.setClickHandler(this);
        binding.setLifecycleOwner(this);

        setToolbar(binding.toolbar, true, R.string.terms_of_service);

        initWebview();
        binding.webView.loadUrl(URL);

        Log.d(TAG, "onCreate");
    }

    private void initWebview() {

        binding.webView.setWebViewClient(new WebViewClient(){

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }

            @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public void onPageStarted(
                    WebView view, String url, Bitmap favicon) {

                try {
                    // showProgressDialog();
                } catch (Exception ignore) {

                }


            }

            @Override
            public void onPageFinished(WebView view, String url) {

                try {
                    // hideProgressDialog();
                } catch (Exception ignore) {

                }


            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                //Your code to do
                try {
                    // hideProgressDialog();
                } catch (Exception ignore) {

                }
            }

        });

        binding.webView.setHorizontalScrollBarEnabled(false);
        binding.webView.getSettings().setJavaScriptEnabled(true);
        binding.webView.getSettings().setUseWideViewPort(true);
        binding.webView.setInitialScale(1);
        binding.webView.getSettings().setLoadWithOverviewMode(true);

    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onCheckChange(View view, boolean isChecked) {

    }


    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

I think this problem should be related to context. You should be using the context of the Activity. it seems you instantiated webview in DataBindingUtil class with the wrong context. Please pass the context that is WebViewActivity class to your Webview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM