latata
latata

Reputation: 1723

Android app custom dialogs architecture

I have a MainActivity with some Fragments changed by Navigation Drawer. I would like to prepare some custom Dialogs and want to open them by my custom class (neither activity nor fragment class). Is it possible to do something like this without implementing method for each custom Dialog in MainActivity or Fragment?

Upvotes: 0

Views: 65

Answers (1)

BadIdeaException
BadIdeaException

Reputation: 2124

I'm not sure I understand 100% what the problem is, but if it's with having many dialogs which you don't want to set up using many different methods in your activity, have the dialogs inherit from a new class MyDialogFragment with a static newInstance method, then pass the specific dialog class you want to instantiate as a parameter.

So

public class MyDialogFragment extends DialogFragment {
    ...
    public static DialogFragment newInstance() {
        // Overwrite this method in your dialogs to do what you have to do
    }
}

Then in your activity:

private void instantiateDialog(Class<? extends MyDialogFragment dialogClass) {
    MyDialogFragment dialog = c.newInstance();
    dialog.show(getFragmentManager(), c.getCanonicalName());
}

Upvotes: 2

Related Questions