1

I am working on App where i am attaching 5 fragments on an activity. Everything is working great but when i put my app in background from any fragment and after some time when my app resumes it crashes. I get reference of my Activty as null. here is my code

This is code in Activty from where i am attaching fragment

   FragmentManager fragmentManager = getSupportFragmentManager();
   searchFragment = SearchFragment.newInstance(MainBaseActivity.this);
   fragmentTransaction = fragmentManager.beginTransaction();
   fragmentTransaction.add(R.id.frameLayoutMain, searchFragment, "SearchFragment");
   fragmentTransaction.commit();

And this is my Fragment class

   public static SearchFragment newInstance(MainBaseActivity mainBaseActivity) {

    fragment = new SearchFragment();
    fragment.mainBaseActivity = mainBaseActivity;
    fragment.inflater = (LayoutInflater) mainBaseActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    fragment.myApplication = ((MyApplication) mainBaseActivity.getApplicationContext());
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.search_fragment, container, false);

    preferences = mainBaseActivity.getSharedPreferences(Constant.CHAI_APP, Context.MODE_PRIVATE);  // here i get null pointer
    editor = preferences.edit();

    return view;
}
1
  • You know. I really like stacktraces provided with questions. So please post it. Commented Nov 11, 2014 at 11:30

2 Answers 2

1

Fragments can be killed and recreated by the system at various times. You cannot trust the kind of initialization you do in your newInstance() - when the fragment is recreated, the fields won't be initialized.

Remove these initializations:

fragment.mainBaseActivity = mainBaseActivity;
fragment.inflater = (LayoutInflater) mainBaseActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
fragment.myApplication = ((MyApplication) mainBaseActivity.getApplicationContext());

and use getActivity() in your fragment when you need to access your hosting activity or the Application.

For inflater, one is already passed in as an argument to onCreateView(). No need to fetch it yourself.

(To pass params to a fragment that persist over fragment recreation, use setArguments().)

1
  • Thanks for your suggestion i ll do as u suggested.
    – Avinash
    Commented Nov 11, 2014 at 11:41
1

Fragment has the getActivity() method to retrieve the activity to which it is attached. Use it in place of mainBaseActivity

5
  • I used that but got it as null also
    – Avinash
    Commented Nov 11, 2014 at 11:33
  • I don't see it used anywhere
    – Blackbelt
    Commented Nov 11, 2014 at 11:34
  • I meant i used getActivity() at the place of mainbaseactivity but got nullpointer in that case also
    – Avinash
    Commented Nov 11, 2014 at 11:35
  • where did you used it? Inside onCreateView or inside newInstance ?
    – Blackbelt
    Commented Nov 11, 2014 at 11:36
  • inside onCreateView()
    – Avinash
    Commented Nov 11, 2014 at 11:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.