开始新的Activity时,Android Activity Lifecycle调用了两次

时间:2018-11-21 08:53:30

标签: android android-lifecycle activity-lifecycle

我基本上是在开始包含WebView的新Activity。但是,此WebView Activity的生命周期在首次运行时调用了两次。销毁此活动并重新打开时,一切正常。

活动A运行

Act A -> onCreate

Act A -> onStart

Act A -> onResume

开始新的活动

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

活动B(WebView活动)运行

Act A -> onPause

Act B -> onCreate

Act B -> onStart

Act B -> onResume

日志信息可能是导致这种情况的原因

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

然后继续

Act B -> onPause

Act B -> onStop

Act B -> onDestroy

Act B -> onCreate

Act B -> onStart

Act B -> onResume

Act A -> onStop

我已经用很多设备测试了这种情况

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

是设备错误吗?或操作系统错误?我该如何解决?如果有人可以提供帮助,我将不胜感激。

我的WebView活动代码

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");
    }

1 个答案:

答案 0 :(得分:0)

我认为这个问题应该与背景有关。您应该使用活动的上下文。看来您使用错误的上下文实例化了DataBindingUtil类中的webview。请将WebViewActivity类的上下文传递给您的Webview