From the accepted answer to this question, I building an AlertDialog that I can use several times in my Activity but I having a problem with the positive button.
Have been trying to supply the right context to line (!NetworkCheck.isAvailableAndConnected(getApplicationContext())) {
in the code below:
MainActivity
public class MainActivity extends AppCompatActivity {
private final String TAG = "MainActivity";
private AlertDialog internetDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
//The usuals
Log.d(TAG, "onCreate called");
internetDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.alert_titl)
.setCancelable(false)
.setIcon(R.mipmap.ic_launcher)
.setMessage(R.string.alert_mess)
.setPositiveButton(R.string.alert_retry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!NetworkCheck.isAvailableAndConnected(getApplicationContext())) {
internetDialog.show();
} else {
getData();
}
}
})
.setNegativeButton(R.string.alert_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.create();
if (NetworkCheck.isAvailableAndConnected(this)) {
getData();
} else {
internetDialog.show();
}
}
//This method will get data from the web api
private void getData(){
Log.d(TAG, "getData called");
//Showing progress dialog
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setCancelable(false);
mProgressDialog.setMessage(this.getResources().getString(R.string.load_post));
mProgressDialog.show();
//Creating a json request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ConfigPost.GET_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, "onResponse called");
//Dismissing the progress dialog
if (mProgressDialog != null) {
mProgressDialog.hide();
}
/*progressDialog.dismiss();*/
//calling method to parse json array
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (mProgressDialog != null) {
mProgressDialog.hide();
}
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}
NetworkCheck
public class NetworkCheck {
public static boolean isAvailableAndConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isNetworkAvailable = cm.getActiveNetworkInfo() != null;
boolean isNetWorkConnected = isNetworkAvailable && cm.getActiveNetworkInfo().isConnected();
return isNetWorkConnected;
}
}
The negative button is working quite alright but the positive button only succeeds in dismissing the dialog. getData
is not called (if there is an internet connection) neither is the the dialog shown again (if there is no internet connection).
Please, what is the right context (if that's the problem) to supply to (!NetworkCheck.isAvailableAndConnected(getApplicationContext())) {
to make the positive button work? I have tried getApplicationContext
, getBaseContext
and MainActivity.this
but it didn't work.
If I do this:
if (NetworkCheck.isAvailableAndConnected(this)) {
//Caling method to get data
getData();
} else {
final Context mContext;
mContext = this;
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle(R.string.alert_titl);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setIcon(R.mipmap.ic_launcher);
alertDialogBuilder.setMessage(R.string.alert_mess);
alertDialogBuilder.setPositiveButton(R.string.alert_retry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!NetworkCheck.isAvailableAndConnected(mContext)) {
alertDialogBuilder.show();
} else {
getData();
}
}
});
alertDialogBuilder.setNegativeButton(R.string.alert_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialogBuilder.show();
}
the retry button calls getData
if there is an internet connection and re-shows the dialog if there is no connection.
I could have used this approach but I cant't show the dialog in another method if I wanted to.