-1

I am trying to save values in my pref before fragment is destroyed but an error message says

android.support.v4.app.SuperNotCalledException: Fragment did not call through to super.onDestroyView()

here is my code

@Override
public void onDestroyView() {
    pref3.edit().clear().commit();
    for (int i = 0; i < movies1.size(); i++) {
        favouritemovies1.add(movies1.get(i));
        SharedPreferences.Editor editor3 = pref3.edit();
        editor3.putStringSet("favouritemovies", favouritemovies1);
        editor3.commit();
        Toast.makeText(getActivity(), "destroyed", Toast.LENGTH_SHORT).show();
        super.onDestroyView();

    }
}

when I comment for loop every thing works fine!

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98

2 Answers2

3

Just keep the super.onDestroyView(); out of the for loop and inside the @Override method like this:-

   @Override
    public void onDestroyView() {
        // or here 
        pref3.edit().clear().commit();
        for (int i = 0; i < movies1.size(); i++) {
            favouritemovies1.add(movies1.get(i));
            SharedPreferences.Editor editor3 = pref3.edit();
            editor3.putStringSet("favouritemovies", favouritemovies1);
            editor3.commit();
            Toast.makeText(getActivity(), "destroyed", Toast.LENGTH_SHORT).show();
    
        }
        super.onDestroyView();
    }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
A S M Sayem
  • 2,010
  • 2
  • 21
  • 28
2

Remove super.onDestroyView(); from the loop and write it in the top like following.

@Override
public void onDestroyView() {
    super.onDestroyView();
    pref3.edit().clear().commit();
    for (int i = 0; i < movies1.size(); i++) {
        favouritemovies1.add(movies1.get(i));
        SharedPreferences.Editor editor3 = pref3.edit();
        editor3.putStringSet("favouritemovies", favouritemovies1);
        editor3.commit();
        Toast.makeText(getActivity(), "destroyed", Toast.LENGTH_SHORT).show();
        

    }
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29