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.