I'm trying to follow a given example by the user @sunil-sunny, that says
You can create static varibales like this
static FragmentB f; public static FragmentB newInstance(String title) { FragmentB f = new FragmentB(); Bundle b = new Bundle(); b.putString(ARG_STATION_TITLE, title); f.setArguments(b); return f; }
You can use the
getInstance()
method to get the instance of fragmentBpublic static FragmentB getInstance(){ return f; }
Call like this
FragmentB.getInstance().methodinFragmentB();
But for some reason I'm getting a NullPointerException
.
In my Fragment, I have:
static ProductListFragment fragment;
public static ProductListFragment newInstance(Category category, SubCategory subCategory) {
Bundle args = new Bundle();
args.putParcelable(AppConstants.FLAG_CATEGORY, category);
args.putParcelable(AppConstants.FLAG_SUBCATEGORY, subCategory);
ProductListFragment fragment = new ProductListFragment();
fragment.setArguments(args);
return fragment;
}
public static ProductListFragment getInstance(){
return fragment;
}
And in another fragment I call a method detachFragment
to close the current fragment and reload the list updated in the previous fragment:
// We close the fragment and reload the list
public void detachFragment(String tag) {
transition.reverseTransition(100);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(tag);
if (fragment != null) {
fragmentManager
.beginTransaction()
.disallowAddToBackStack()
.setCustomAnimations(R.anim.slide_up_bottom_sheet, R.anim.slide_down_bottom_sheet)
.remove(fragment)
.commitNow();
ProductListFragment.getInstance().reloadProducts(); // Reload Fragment
}
}
Logcat sais that the next line return a Null:
...
ProductListFragment.getInstance().reloadProducts(); // Reload Fragment
...