我正在开发一个Android项目。我BaseActivity
扩展了AppCompatActivity
,而我的另一个活动StartActivity
扩展了BaseActivity
。我在BaseActivity中有AlertDialog,我正在调用它。
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setCancelable(false);
alertDialog.setTitle(getResources().getString(R.string.title_attention));
alertDialog.setMessage(getResources().getString(R.string.message_permission_denied));
alertDialog.setPositiveButton(getResources().getString(R.string.button_settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + getApplicationContext().getPackageName()));
myAppSettings.addCategory(Intent.CATEGORY_DEFAULT);
myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(myAppSettings);
}
});
alertDialog.setNegativeButton(getResources().getString(R.string.button_exit),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
aDialog.dismiss();
finish();
}
});
aDialog = alertDialog.create();
aDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
aDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorButtonPositive));
aDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorButtonNegative));
}
});
if (!aDialog.isShowing())
aDialog.show();
我也在BaseActivity中覆盖onDestroy
方法,
@Override
protected void onDestroy() {
super.onDestroy();
try{
runOnUiThread(new Runnable() {
@Override
public void run() {
if (aDialog != null && aDialog.isShowing()) {
aDialog.dismiss();
}
}
});
}catch (Exception e){
new ErrorPrinter(e.toString());
}
}
StackTrace /错误日志,
01-13 08:38:59.928 5501-5501/com.company.android E/WindowManager: android.view.WindowLeaked: Activity com.company.android.activities.StartActivity has leaked window com.android.internal.policy.PhoneWindow$DecorView{7a59bc1 V.E...... R......D 0,0-1160,413} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:368)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:299)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:319)
at com.company.android.activities.BaseActivity.permissionDenied(BaseActivity.java:216)
at com.company.android.activities.BaseActivity.onRequestPermissionsResult(BaseActivity.java:160)
at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6582)
at android.app.Activity.dispatchActivityResult(Activity.java:6460)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
但我在Android Monitor中遇到异常,StartActivity已经泄露了最初添加的窗口。谁能告诉我怎样才能解决这个错误?
我扩展到BaseActivity的原因是我想为所有活动使用一些功能,所以我可以编写或删除一次代码,它将用于所有活动。
感谢。
答案 0 :(得分:0)
由于活动暂停时,它是垃圾收集的,但由于对话框仍然没有被解除,警报对话框仍然有对活动的引用。
您需要覆盖BaseActivity中的onPause()和onStop():
@Override
public void onPause() {
super.onPause();
if(aDialog != null)
aDialog.dismiss();
}
@Override
protected void onStop() {
super.onStop();
if(aDialog != null)
aDialog.dismiss();
}
当其他活动开始时,同时关闭对话框并在alertDialog.setPositiveButton()
中的此行之后完成活动:
getApplicationContext().startActivity(myAppSettings);
aDialog.dismiss();
finish();
答案 1 :(得分:0)
我必须覆盖finish()
方法并通过获取进程ID来终止我的进程。
@Override
public void finish() {
super.finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
谢谢大家的帮助和支持!