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;
}