Reputation: 19297
I created an AlertDialog
:
public class ConfirmChoixDownloadDialogView extends AlertDialog {
private Activity activity; // this activity shows this Dialog
private View contenu, titleBar;
private final int OUI = DialogInterface.BUTTON_POSITIVE;
private final int NON = DialogInterface.BUTTON_NEGATIVE;
private final int NEUTRE = DialogInterface.BUTTON_NEUTRAL;
public static final int requestCode = 100;
private int btnClicked = NEUTRE;
private AsyncTask<String, Void, Void> indirectTask;
@SuppressLint("InlinedApi")
public ConfirmChoixDownloadDialogView(Context context, LayoutInflater inflater) {
super(context, AlertDialog.THEME_HOLO_DARK);
contenu = inflater.inflate(R.layout.msg_dialog, null);
((TextView)contenu.findViewById(R.id.msgText)).setText(context.getResources().getString(R.string.questionChargementWebVersMobile));
titleBar = inflater.inflate(R.layout.custom_dialog_title, null);
((ImageView)titleBar.findViewById(R.id.icone)).setImageDrawable(context.getResources().getDrawable(R.drawable.ic_action_about));
((TextView)titleBar.findViewById(R.id.titre)).setText(context.getResources().getString(R.string.titreConfirmMsgBox));
setCustomTitle(titleBar);
setView(contenu, 0, 0, 0, 0);
setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(R.string.button_oui), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
btnClicked = OUI;
dialog.dismiss();
}
});
setButton(DialogInterface.BUTTON_NEGATIVE, context.getResources().getString(R.string.button_non), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
btnClicked = NON;
dialog.dismiss();
}
});
setOnDismissListener(dismissListener);
}
public void afficher(Activity act, AsyncTask<String, Void, Void> tacheIndirecte) {
activity = act;
indirectTask = tacheIndirecte;
show();
}
private OnDismissListener dismissListener = new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (dialog instanceof ConfirmChoixDownloadDialogView) {
if (btnClicked == OUI) {
indirectTask.execute("commune.txt",
"parcelles.txt",
"rues.txt",
"batiments.txt",
"appartements.txt",
"occupants_appartement.txt",
"categories_activite.txt",
"natures_activite.txt",
"activites_dans_appart.txt",
"contribuables.txt");
}
else if (btnClicked == NON) {
Intent i = new Intent(activity, SelectionQuartierActivity.class);
activity.startActivity(i); // it makes app crash
}
}
}
};
}
As you can see I want to start an activity
in the case when the BUTTON_NEGATIVE
of the Dialog
is clicked. But it crash ! So how to start the activity
in this case ?
Upvotes: 0
Views: 95
Reputation: 19297
Ok , I found a tip to start the activity
from the activity
which opened the Dialog
by implementing the onWindowFocusChanged
method of the activity
and setting a public static
variable of the activity
inside the OnDismissListener
of the Dialog
!
Upvotes: 0
Reputation: 5323
In your case maybe try using the context of the AlertDialog instead of the Activity calling it like this:
context.startActivity(i);
instead of activity.startActivity(i)
Upvotes: 0
Reputation: 3723
showDialog(this, "", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(getApplicationContext(), NextActivty.class);
startActivity(i);
finish();
}
});
Upvotes: 1