Get access to getSupportFragmentManager in non activity class
Asked Answered
M

1

7

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);
..
   }
}
Merge answered 21/11, 2017 at 17:14 Comment(1)
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.Cirrocumulus
S
7

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");
..
   }
Sacrosanct answered 21/11, 2017 at 17:17 Comment(1)
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.Merge

© 2022 - 2024 — McMap. All rights reserved.