0

I have problems showing a DialogFragment on an activity. I found a lot of other posts about this issue, but I couldn't find a solution.

This is my Activity:

public class MainActivity extends FragmentActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      // check whether data connection is available
      ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo netInfo = cm.getActiveNetworkInfo();
      if (netInfo == null || !netInfo.isConnectedOrConnecting()) {

         DataConnectionUnavailableDialog dialog = DataConnectionUnavailableDialog
               .newInstance("R.string.connection_unavailable");
         dialog.show(getFragmentManager(), "");
      }
   < other stuff >
   }
< other stuff >
}

As you can see I want to display a dialog when Internet connection is not available.

This is my dialog.

public class DataConnectionUnavailableDialog extends DialogFragment {

   static DataConnectionUnavailableDialog newInstance(String title) {
      DataConnectionUnavailableDialog f = new DataConnectionUnavailableDialog();
      Bundle args = new Bundle();
      args.putString("title", title);
      f.setArguments(args);
      return f;
   }

   @Override
   public Dialog onCreateDialog(Bundle savedInstanceState) {
      // Use the Builder class for convenient dialog construction
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      builder.setMessage(R.string.connection_unavailable).setPositiveButton(
            R.string.open_connection, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                  try {
                     < ... >
              } catch (Exception e) {
                 throw new RuntimeException(e.toString());
              }
           }
        }).setNegativeButton(R.string.cancel,
        new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
              finish();
           }
        });
      // Create the AlertDialog object and return it
      return builder.create();
   }   
}

The strange thing is that few months ago, the dialog appeared. Then I left the project and when I returned to it, after some other modifications, I found this issue.

I'm on a 4.0.3 Android.

sthor69
  • 638
  • 1
  • 10
  • 25
  • `System.exit(0)` this is wrong you need to call `finish` – Raghunandan Aug 28 '13 at 12:01
  • Thanks for the comment. I used what I'm used to in Java, but I was sure it wasn't the correct way to do it. – sthor69 Aug 28 '13 at 13:36
  • Aren't there anyone who can give me some hints on where the issue can be? – sthor69 Sep 11 '13 at 09:48
  • I found the issue, ad it was due to lack of experience in the Android thread management. The dialog fragment paradigm is different in terms of user experience and thread management and this could lead to confusion newbie like me: when a new DialogFragment is displayed, the main thread is not stopped waiting for the user answer. So if code following the DialogFragment.show() need the user input to work, it can break. – sthor69 Sep 18 '13 at 12:58

0 Answers0