自定义片段的 subclass 是否调用其 super class 的 saveInstanceState 方法?
Does a subclass of a custom fragment call the saveInstanceState method of its super class?
所以我有一个 class 扩展片段
public class General_Fragment extends Fragment{
int i;
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putInt(INTEGER_KEY, getInt());
super.onSaveInstanceState(outState);
}
public int getInt(){
return i;
}
…
}
还有另一个 class 扩展了第一个 class。
public class Specific_Fragment extends General_Fragment{
int j;
@Override
public int getInt(){
return j;
}
…
}
当特定片段被销毁时,应该调用 General_Fragments onSavedInstanceState 是否正确?或者我需要在 subclass 中实现一些东西来调用 superclass 方法吗?
如果您不覆盖 Specific_Fragment
中的 onSaveInstanceState
方法,将调用 General_Fragment
中实现的方法。
所以没有必要覆盖它,只要你不需要保存来自最派生片段的其他东西。
不,您不需要重写它即可调用,它是从其父项 class 继承而来的,即 General_Fragment
所以我有一个 class 扩展片段
public class General_Fragment extends Fragment{
int i;
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putInt(INTEGER_KEY, getInt());
super.onSaveInstanceState(outState);
}
public int getInt(){
return i;
}
…
}
还有另一个 class 扩展了第一个 class。
public class Specific_Fragment extends General_Fragment{
int j;
@Override
public int getInt(){
return j;
}
…
}
当特定片段被销毁时,应该调用 General_Fragments onSavedInstanceState 是否正确?或者我需要在 subclass 中实现一些东西来调用 superclass 方法吗?
如果您不覆盖 Specific_Fragment
中的 onSaveInstanceState
方法,将调用 General_Fragment
中实现的方法。
所以没有必要覆盖它,只要你不需要保存来自最派生片段的其他东西。
不,您不需要重写它即可调用,它是从其父项 class 继承而来的,即 General_Fragment