Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android display another dialog from a dialog

Tags:

android

dialog

I am trying to display a dialog from the onClick listener of a button of another dialog, but the 2nd dialog won't display. I searched and found a similar problem- Dialogs order in Android, tried the solution provided, but even that does not work.

My code is very similar to the one provided in the answer.

public void onClick(DialogInterface dialog, int id) { showDialog(SECOND_DIALOG); dialog.dismiss(); }

any help will be really appreciated.

Thanks,

Akshay

like image 237
Akshay Avatar asked Apr 14 '11 11:04

Akshay


2 Answers

This is how I'm doing it:

    if (!appPrefs.getAcceptedUsageAggrement()) {
        tracker.trackPageView("/UsageAgreementDialog");
        acceptedUsage_alertDialog = new AlertDialog.Builder(BroadcastSMSActivity.this)
        .setTitle(R.string.accept_usage_title)
        .setMessage(R.string.accept_usage_message)
        .setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        })
        .setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (appPrefs.getAppVer().equals("")) {
                    tracker.trackEvent("Application", "Install", getAppVerName(), 1);
                } else {
                    tracker.trackEvent("Application", "Upgrade", appPrefs.getAppVer().toString()+"->"+getAppVerName(), 1);
                }
                displayRecentChanges = true;
                appPrefs.saveAppVer(getAppVerName());
                appPrefs.saveAcceptedUsageAggrement(true);
            // Display Recent Changes on 1st use of new version
                if (displayRecentChanges) {
                    tracker.trackPageView("/RecentChangesDialog");
                    recentChanges_alertDialog = new AlertDialog.Builder(BroadcastSMSActivity.this)
                    .setTitle(getString(R.string.changes_title, getAppVerName()))
                    .setMessage(R.string.changes_dialog)
                    .setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            recentChanges_alertDialog.cancel();
                            acceptedUsage_alertDialog.cancel();
                        }
                    })
                    .create();
                    recentChanges_alertDialog.show();
                }
            }
        })
        .create();
        acceptedUsage_alertDialog.show();
    }
like image 143
Bill Mote Avatar answered Sep 21 '22 20:09

Bill Mote


I know this was asked a while ago but here's a pretty neat solution I found.

I define an interface like this:

public interface OpenDialog {

public void showDialog(DialogFragment dialog);

}

Which my activity then implements, passing a reference to itself to the dialog when it's opened, using my InterfaceHolder class:

public class MyActivity extends FragmentActivity implements OpenDialog {

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);

    InterfaceHolder.set(this);        
    showDialog(new DialogOne());

}

public void showDialog(DialogFragment dialog) {
    dialog.show(getSupportFragmentManager(), "tag");
}

InterfaceHolder is just a class with a static reference to the interface I use to pass it around:

public class InterfaceHolder {
private static OpenDialog openDialog;

public void set(OpenDialog openDialog)
    this.openDialog = openDialog;
}

public void get()
    return openDialog;
}

So the showDialog method will display any dialog that I pass into it; as you can see, I do this to display DialogOne. Now, if I want to open a new dialog called "DialogTwo" inside "DialogOne" I can call it by writing:

InterfaceHolder.get().showDialog(new DialogTwo());
dismiss();

And voila, DialogTwo is shown. Obviously, you have to be careful to ensure that a reference to your activity has been passed to the InterfaceHolder (a nice way to do this is to put InterfaceHolder.set(this); inside the showDialog method), but otherwise this seems to work beautifully.

like image 29
public static void Avatar answered Sep 20 '22 20:09

public static void