gpt4 book ai didi

Android Activity 生命周期在启动新 Activity 时调用两次

转载 作者:太空狗 更新时间:2023-10-29 14:37:38 26 4
gpt4 key购买 nike

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

Activity A运行

Act A -> onCreate

Act A -> onStart

Act A -> onResume

开始新 Activity

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

Activity B(WebView Activity)运行

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 Activity 代码

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

最佳答案

我觉得这个问题应该跟语境有关。您应该使用 Activity 的上下文。您似乎在 DataBindingUtil 类中使用错误的上下文实例化了 webview。请将 WebViewActivity 类的上下文传递给您的 Webview

关于Android Activity 生命周期在启动新 Activity 时调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53408314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com