取消AlertDialog和应用程序

时间:2013-06-27 06:08:42

标签: android android-alertdialog

我正在使用一个Activity,我在其中声明了ActionBar选项卡和ViewPager。每个页面都在线获取数据并在ListView中显示它。我没有网络连接时显示AlertDialog。问题是AlertDialog没有被取消一键。有六个选项卡和相同的ViewPager,点击它后六次被取消。

以下是完整的代码。

public class AlertDialogBox  extends DialogFragment{
Context mContext;
StartActivity sa;
public AlertDialogBox(Context context) {
    mContext = context;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    sa =new StartActivity();
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
    //alertDialogBuilder.setTitle("Really?");
    alertDialogBuilder.setMessage("Check your internet connection");
    //null should be your on click listener
   //alertDialogBuilder.setPositiveButton("OK", null);
    alertDialogBuilder.setNegativeButton("OK", new DialogInterface.OnClickListener() {

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

            //dialog.dismiss();
            //mContext.
            //mContext.finish();
            sa.finish();

        }
    });


    return alertDialogBuilder.create();
}
}

1 个答案:

答案 0 :(得分:0)

在主要活动中移动您的互联网连接检查,而不是在每个页面中。另外,从主要活动中显示对话框。

public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
    alertDialogBuilder.setMessage("Check your internet connection");       
    alertDialogBuilder.setNegativeButton("OK", new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
     dialog.dismiss();
     finish();      

   }
});
return alertDialogBuilder.create();

}