我的应用是在主要活动的OnCreate
功能中检查启动时的版本兼容性。由于Android没有(据我所知)有模态对话框,如果版本不兼容,我会通过AlertDialog
显示AlertDialog.Builder
并设置中性按钮操作以关闭应用。不幸的是,这导致应用程序重新启动而不是关闭。有人可以帮我把应用程序关闭吗?
这是在Android 7.0上测试的,但也需要在Android 4.2.2上运行。
public class Activity1 : FragmentActivity {
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
if (!versionCheck.Success) {
ShowNotesReadonlyDialog("Version Incompatibility", versionCheck.Message, new Action(() => ShutdownApplication()) );
return;
}
}
private void ShutdownApplication() {
this.Finish();
// -- I've tried all the things below with the same result.
//this.FinishAffinity();
//this.Dispose();
//global::Android.OS.Process.KillProcess(global::Android.OS.Process.MyPid());
}
private void ShowNotesReadonlyDialog(String title, String message, Action action) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
View layout = inflater.Inflate(Resource.Layout.GenericTextViewLayout, FindViewById<ViewGroup>(Resource.Id.GenericTextViewDialogRoot));
dialog.SetTitle(title);
dialog.SetView(layout);
TextView label = layout.FindViewById<TextView>(Resource.Id.GenericTextViewMessageLabel);
label.Text = message;
dialog.SetNeutralButton("Close", (o, e) => {
if (null != action) {
action.Invoke();
}
dialog.Dispose();
});
dialog.SetCancelable(false);
AlertDialog window = dialog.Create();
WindowManagerLayoutParams p = new WindowManagerLayoutParams();
p.CopyFrom(window.Window.Attributes);
p.Width = 900;
p.Height = WindowManagerLayoutParams.WrapContent;
window.Show();
window.Window.Attributes = p;
}
}
我在几个生命周期事件中放置了断点。调用Finish()
后,在Activity上调用以下事件:
OnStop()
OnCreate()
OnStart()
此外,当调用Finish()
时,活动会从屏幕上消失,但按下&#34;窗口管理器&#34;硬件密钥显示应用程序仍在运行。点击窗口会在屏幕上显示它。