如何从处理程序完成活动?

发布于 2024-12-20 11:01:07 字数 1473 浏览 2 评论 0原文

我正在从处理程序调用警报对话框。警报对话框有 2 个按钮“再次播放”和“退出”。 我已经编写了代码以在“再次播放”按钮中重新启动活动。但当我单击退出按钮时,我不知道如何完成应用程序。我无法从我的处理程序中调用 finish ()。下面是我的代码。请任何人帮助我...

private Handler handler = new Handler() {
     public void handleMessage(Message msg) {
         AlertDialog.Builder builder = new AlertDialog.Builder(_context);
            builder.setMessage("Game Over !!!")
                    .setCancelable(false)
                    .setPositiveButton("Play Again",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,int which) {
                                    //thread.start();
                                    Intent intent = new Intent ( _context , DroidzActivity.class );                                    
                                    _context.startActivity ( intent ); 
                                }
                            })
                    .setNegativeButton("Exit",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,int which) {

                                    dialog.cancel(); 

                                }
                            });
            AlertDialog alert = builder.create();
            alert.show();
           }




            };

i am calling Alert dialog froma handler.Alert dialog has 2 buttons "play Again" and "Exit".
i have written the code to restart the activity in "play again" button. but i dont know how to finish the application when i click on exit button. i cant call finish () from my handler.given below is my code.please anybody help me...

private Handler handler = new Handler() {
     public void handleMessage(Message msg) {
         AlertDialog.Builder builder = new AlertDialog.Builder(_context);
            builder.setMessage("Game Over !!!")
                    .setCancelable(false)
                    .setPositiveButton("Play Again",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,int which) {
                                    //thread.start();
                                    Intent intent = new Intent ( _context , DroidzActivity.class );                                    
                                    _context.startActivity ( intent ); 
                                }
                            })
                    .setNegativeButton("Exit",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,int which) {

                                    dialog.cancel(); 

                                }
                            });
            AlertDialog alert = builder.create();
            alert.show();
           }




            };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿透光 2024-12-27 11:01:07

如果您想要完成的活动中有上述代码,请尝试以下代码:

YourActivity.this.finish();

编辑
我建议不要破坏当前活动,而是以以下方式开始:

Intent intent = new Intent ( _context , DroidzActivity.class );    
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
_context.startActivity ( intent ); 

并在 onNewIntent() 方法中处理“再次播放”操作。您需要在您的活动中覆盖它。

有关更多参考,请检查: http://developer.android.com/reference /android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP

http://developer.android.com/reference/android /app/Activity.html#onNewIntent(android.content.Intent)

In case your above code is in your activity which you would like to finish, try the following code:

YourActivity.this.finish();

Edit:
I propose not destroying the current activity but starting in the following way:

Intent intent = new Intent ( _context , DroidzActivity.class );    
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
_context.startActivity ( intent ); 

And handle "play Again" action in onNewIntent() method. You will need to override it in your activity.

For more reference please check: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP

and

http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文