onDestroyView() 方法有什么问题?

What is wrong with onDestroyView() method?

我试图在片段被销毁之前将值保存在我的 pref 中,但一条错误消息显示

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

这是我的代码

@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();

    }
}

当我评论循环时,一切正常!

从循环中删除 super.onDestroyView(); 并将其写在顶部,如下所示。

@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();
        

    }
}

只需将 super.onDestroyView(); 置于 for 循环之外并置于 @Override 方法内,如下所示:-

   @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();
    }