如何从处理程序完成活动?
我正在从处理程序调用警报对话框。警报对话框有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要完成的活动中有上述代码,请尝试以下代码:
编辑:
我建议不要破坏当前活动,而是以以下方式开始:
并在 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:
Edit:
I propose not destroying the current activity but starting in the following way:
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)