6

I hope someone can help. I have a non activity class in which I need to access the fragmentmanager in a static method, unfortunatly I don`t know how to pass the context correctly.

Any help is appreciated,

Non activity class:

public class testClass {
..
public synchronized static void checkNewdata(List<data>input, Context context) {
..
       FragmentManager ft = ((FragmentActivity)???).getSupportFragmentManager();
                DialogFragment newFragment = MyNotification.newInstance();
                newFragment.setCancelable(false);
                newFragment.show(ft, "mydialog");
..
   }
}

The method checkNewdata is also being called from a non activity class, in which I try to pass context but this doesn`t work:

public class SearchResponse extends SuccessResponse {

private Context mcontext;

public SearchResponse(Context context){
    mcontext = context;
}
..
@Override
    public void save() {
..
    testClass.checkNewdata(mainData, context);
..
   }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Simon
  • 1,691
  • 2
  • 13
  • 28
  • Holding a `Context` reference in a static method is a bad idea. I suggest you to rethink your design rather than searching for a workaround. – Neria Nachum Nov 21 '17 at 17:25

1 Answers1

6

Pass your Activity instead of Context

public synchronized static void checkNewdata(List<data>input, Activity activity) {
..
       FragmentManager ft = ((FragmentActivity)activity).getSupportFragmentManager();
                DialogFragment newFragment = MyNotification.newInstance();
                newFragment.setCancelable(false);
                newFragment.show(ft, "mydialog");
..
   }
diegoveloper
  • 93,875
  • 20
  • 236
  • 194
  • Thx, I used: private Activity activity; public SearchResponse(Activity activity) { this.activity = activity; } But now the app crashes: Unable to create converter for class.. retrofit. – Simon Nov 21 '17 at 17:43