【问题标题】:Does a subclass of a custom fragment call the saveInstanceState method of its super class?自定义片段的子类是否调用其超类的 saveInstanceState 方法?
【发布时间】:2018-02-20 14:07:38
【问题描述】:

所以我有一个扩展片段的类

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

    …
}

还有一个扩展这个第一类的类。

public class Specific_Fragment extends General_Fragment{
    int j;

    @Override
    public int getInt(){
        return j;
    }

    …
}

当特定片段被销毁时,General_Fragments onSavedInstanceState 应该被正确调用吗?还是我需要在子类中实现一些东西来调用超类的方法?

【问题讨论】:

    标签: android android-fragments superclass


    【解决方案1】:

    如果您不覆盖Specific_Fragment 中的onSaveInstanceState 方法,则会调用General_Fragment 中实现的方法。

    所以没有必要覆盖它,只要你不需要从最派生的片段中保存其他东西。

    【讨论】:

      【解决方案2】:

      不,你不需要重写它来调用它,它继承自它的父类General_Fragment

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-28
        • 1970-01-01
        • 2011-11-22
        • 1970-01-01
        • 2011-10-24
        • 2023-03-08
        • 2012-04-18
        相关资源
        最近更新 更多