Reputation: 145
My app works fine on most devices, except in some, where even in the fragment's onActivityCreated, the getActivity method keeps returning null
. I need Context class to set-up things.
So anyone can help me?
public class BaseProductFragment extends Fragment {
...
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(layout_id, container,
false);
return rootView;
}
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ImageCacheParams cacheParams = new ImageCacheParams(getActivity(),
Utils.PRODUCTS_CACHE_DIR);
....
}
Upvotes: 2
Views: 2683
Reputation: 14820
getActivity
"might" return null
when called from within onActivityCreated
...especially during a configuration change like orientation change because the activity gets destroyed...move that initialization to onAttach
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//initialize here
}
onActivityCreated
is a called when the parent activity's onCreate
is called...but remember that it could be "recreated", destroyed during a config change
Upvotes: 6