0

I'm trying to create a method to pass Fragment classes to in order to show a dialog box for convenience purposes.

below is my attempt to do so but I keep getting errors likes ERR1: (Unknown class: 'c') and ERR2: (Cannot resolve method 'show(android.app.FragmentManager, java.lang.String)'.

The plan is to pass it to an OnClickListener() for usage.

public void showDialog(View v, Class c){
    android.app.FragmentManager manager = getFragmentManager();
    //*ERR1*
    c pop = new Pop();
    //*ERR2*
    pop.show(manager, "Pop");
}

Thank you.

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
  • Why do you need to pass a class? Shouldn't it just be `Pop pop = new Pop();` ? – Dawood ibn Kareem Oct 18 '17 at 02:49
  • the one button i have is called "pop" say i wanted to add another fragment class and that is also activated by a button called "unpop" i dont want to have to rewrite some code i'd rather have a function that would say "Unpop unpop = new Unpop()", basically a method that would do it for me – Majid Alashari Oct 18 '17 at 02:55
  • This seems rather superfluous, since showing a `DialogFragment` can be done in one line; e.g., `MyDialogFragment.newInstance(args).show(getFragmentManager(), "tag");`. Anyway, if you really want to do it by passing a `Class`, it would be something like `DialogFragment pop = (DialogFragment) c.newInstance();`, and you'd have to surround that with a `try-catch`. – Mike M. Oct 18 '17 at 03:04
  • You could use the `newInstance` method of `Class`, but I wouldn't recommend it. I'd just have a separate method for each thing you're likely to create. – Dawood ibn Kareem Oct 18 '17 at 03:04
  • can you elaborate on (MyDialogFragment.newInstance(args).show(getFragmentManager()‌​, "tag");) im not sure how to implement that i cant tell the example code from the actual methods sorry >. – Majid Alashari Oct 18 '17 at 03:23
  • A static `newInstance()` method on a `Fragment` class is a common pattern to create instances, but it's something you would need to implement yourself. It's not built in, but it's quite simple to do: https://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment. – Mike M. Oct 18 '17 at 04:46

0 Answers0