我是Android的新手,我遇到了警告对话框的问题。 有这样的方法:
private void deletSelected(){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("are you sure?");
dialog.setPositiveButton("Yes" , new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
int deletedRow = getContentResolver().delete(
ContentUris.withAppendedId(CONTENT_URI , selectedRow) , null , null);
if (deletedRow == -1){
Toast.makeText(EditorActivity.this , "not deleted!" , Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(EditorActivity.this , "deleted successfully" , Toast.LENGTH_SHORT).show();
}
if (dialog != null){
dialog.dismiss();
}
}
});
dialog.setNegativeButton("no" , new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null){
dialog.dismiss();
}
}
});
// Create and show the AlertDialog
AlertDialog alertDialog = dialog.create();
alertDialog.show();
}
和第二种方法:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// User clicked on a menu option in the app bar overflow menu
switch (item.getItemId()) {
// Respond to a click on the "Save" menu option
case R.id.action_save:
save();
finish();
return true;
// Respond to a click on the "Delete" menu option
case R.id.action_delete:
deletSelected();
finish(); // if i remove this line app will work correctly
return true;
}
return super.onOptionsItemSelected(item);
}
我的问题是,如果我在onOptionsItemSelected方法中删除了finish(),我的应用程序将正确运行,但它仍保留在我不想要的当前活动中。我希望用户在确认对话框中选择yes后,在dataBase中选择行并删回主活动,但如果没有选中此行,则行将删除,但应用程序不会返回mainActivity。
如果我添加完成(),确认对话框会在单击删除后立即消失,应用程序将返回主活动而不删除任何内容。 并且在逻辑上显示以下错误:
08-04 21:57:02.630 28064-28064/com.example.android.pets E/WindowManager: android.view.WindowLeaked: Activity com.example.android.pets.EditorActivity has leaked window android.widget.PopupWindow$PopupDecorView{652e345 V.E...... ......ID 0,0-588,144} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:394)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:330)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1279)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1118)
at android.support.v7.widget.AppCompatPopupWindow.showAsDropDown(AppCompatPopupWindow.java:105)
at android.support.v4.widget.PopupWindowCompatKitKat.showAsDropDown(PopupWindowCompatKitKat.java:33)
at android.support.v4.widget.PopupWindowCompat$KitKatPopupWindowImpl.showAsDropDown(PopupWindowCompat.java:129)
at android.support.v4.widget.PopupWindowCompat.showAsDropDown(PopupWindowCompat.java:206)
at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:721)
at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:160)
at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:187)
at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290)
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:175)
at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:803)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:682)
08-04 21:57:02.631 28064-28064/com.example.android.pets E/WindowManager: android.view.WindowLeaked: Activity com.example.android.pets.EditorAc
任何想法?
修改: 删除finish()后将其放在onClick方法中。一切都是正确的。但是我无法理解这两种方式之间的区别是什么?
答案 0 :(得分:3)
尝试将您要完成的活动传递到deletSelected()
方法,然后从activity.finish()
拨打onClick()
,而不是从您目前所在的位置拨打finish()
。< / p>
答案 1 :(得分:1)
这种情况正在发生,因为在您点击optionmenuitemselected后出现对话框时,您将在不关闭AlertDialog的情况下终止活动。因此,用户无法选择或按下&#34;是&#34;或&#34;不&#34;如他所愿。
你需要做的就是在警告对话框的正面按钮上放置onBackPressed(),如果你愿意,可以放在负面。
private void deletSelected(){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("are you sure?");
dialog.setPositiveButton("Yes" , new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
int deletedRow = getContentResolver().delete(
ContentUris.withAppendedId(CONTENT_URI , selectedRow) , null , null);
if (deletedRow == -1){
Toast.makeText(EditorActivity.this , "not deleted!" , Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(EditorActivity.this , "deleted successfully" , Toast.LENGTH_SHORT).show();
}
if (dialog != null){
dialog.dismiss();
}
**// after performing all you task put this
onBackPressed();
// or if you wish to pass some data to previous activity try onActivityResult way.**
}
});
dialog.setNegativeButton("no" , new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null){
dialog.dismiss();
}
}
});
// Create and show the AlertDialog
AlertDialog alertDialog = dialog.create();
alertDialog.show();
}
答案 2 :(得分:0)
用{替换new AlertDialog.Builder(this)
new AlertDialog.Builder(EditorActivity.this);
或使用getApplicationContext()
答案 3 :(得分:0)
就我而言,此解决方案已成功完成: 通过dialog.dismiss()将“ your_activity.this.finish()”放入处理程序中以保持空闲时间。
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
your_activity.this.finish();
}
},500);