1

My app work fine on most device but some rooted device. In fragment onActivityCreated, getActivity keep return null. I need Context class to to set up thing below.

So anyone can help me?

Update

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);
    ....
}
Takashi Lee
  • 135
  • 1
  • 2
  • 10

1 Answers1

5

getActivity "might" return null if 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

Leo
  • 14,284
  • 2
  • 36
  • 54