应用程序在使用system.exit退出应用程序后重新启动。

huangapple go评论59阅读模式
英文:

app restarts after exiting the app using system.exit

问题

我在我的应用程序中创建了一个选项,如果用户点击onBackPress,它将弹出一个对话框,询问他们是否确定要退出应用程序。

一旦他们点击确定,我希望它关闭应用程序,就这样结束。

然而,现在它关闭应用程序,然后重新启动应用程序,并再次返回到相同的屏幕。

我正在使用:

public void popExitDialog(Context context) {

    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.item_warn_exit);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    Button Btn_Continue = dialog.findViewById(R.id.btn_Continue);
    Button Btn_Cancel = dialog.findViewById(R.id.btn_Cancel);
    ImageButton ib_CloseDialog = dialog.findViewById(R.id.ib_CloseDialog);

    dialog.show();

    ib_CloseDialog.setOnClickListener(v -> {
        dialog.dismiss();
    });

    Btn_Cancel.setOnClickListener(view -> {
        dialog.dismiss();
    });

    Btn_Continue.setOnClickListener(view -> {
        AppCompatActivity appCompatActivity = new AppCompatActivity();
        appCompatActivity.finishAndRemoveTask();
        appCompatActivity.finishAffinity();
        System.exit(1);
    });
}

我该如何停止这个自动启动?

谢谢

英文:

I created in my application an option that if the user clicks on the onBackPress that it will pop up a dialog asking if they are sure they want to exit the app.

Once they click ok, I had like it to close the app and thats it.

However, right now it closes the app but then restarts it and returns again to the same screen.

Im using:

public void popExitDialog(Context context) {

    final Dialog dialog = new Dialog( context );
    dialog.setContentView( R.layout.item_warn_exit );
    dialog.getWindow().setBackgroundDrawable( new ColorDrawable( Color.TRANSPARENT ) );

    Button Btn_Continue = dialog.findViewById( R.id.btn_Continue );
    Button Btn_Cancel = dialog.findViewById( R.id.btn_Cancel );
    ImageButton ib_CloseDialog = dialog.findViewById( R.id.ib_CloseDialog );

    dialog.show();

    ib_CloseDialog.setOnClickListener( v -> {
        dialog.dismiss();
    } );

    Btn_Cancel.setOnClickListener( view -> {
        dialog.dismiss();
    } );

    Btn_Continue.setOnClickListener( view -> {
        AppCompatActivity appCompatActivity = new AppCompatActivity();
        appCompatActivity.finishAndRemoveTask();
        appCompatActivity.finishAffinity();
        System.exit( 1 );
    } );
}

How can I stop this auto startup?

Thank you

答案1

得分: 1

((Activity) context).finishAffinity();
System.exit(0);

英文:

Can You try my code, I also used dialog box for exiting my app and it's work fine for me. Hope it will do same for you

((Activity) context).finishAffinity();
                System.exit(0);

答案2

得分: 0

尝试 System.exit(0)

exit(0):通常用于表示成功终止。
exit(1) 或 exit(-1) 或任何其他非零值:通常表示未成功终止。

试试这个:Process.killProcess(Process.myPid());

英文:

try System.exit(0)

exit(0) : Generally used to indicate successful termination.
exit(1) or exit(-1) or any other non-zero value – Generally indicates unsuccessful termination.

Try this : Process.killProcess(Process.myPid());

答案3

得分: 0

你需要从你的主活动中调用 finishAffinity()

你代码中的 AppCompatActivity appCompatActivity = new AppCompatActivity(); 与你的任何活动都没有关系。你基本上只是创建了一个新的、空的活动对象,而该对象在你的应用程序中没有被引用。

如果你从主活动中调用 finishAffinity(),那么这也会关闭所有在任务堆栈中位于其下方的其他活动。因此,你需要从你的“主要(在启动应用程序时运行的第一个活动)-(活动)上下文”中调用 finishAffinity()
然后你可以尝试:

Btn_Continue.setOnClickListener( view -> {
    ((Activity)your_context).finishAffinity();
    System.exit(0);
} );

注意,这段代码在 Android 10 上有效。我刚刚测试过。

英文:

You need to call the finishAffinity() from your main activity!

The AppCompatActivity appCompatActivity = new AppCompatActivity(); in your code is not related to any of your activitys. You're basically just creating a new, empty activity object, that is not referenced in your application.

If you call finishAffinity() from your main activity, than this also closes all other activitys, that are below it in the Task Stack. So you NEED to call finishAffinity() from your
MAIN (first activity running when starting the application)-(Activity) context
Then you can try:

Btn_Continue.setOnClickListener( view -> {
    (Activity)your_context.finishAffinity();
    System.exit(0);
} );

Note that this code is working on Android 10. I just tested it out.

huangapple
  • 本文由 发表于 2020年10月9日 23:31:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64283001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定