Android savedInstanceState返回始终为null

时间:2014-11-04 16:45:52

标签: java android android-fragments android-studio

我有FragmentA和FragmentB,当我回到FragmentA时我在FragmentB中我需要加载最后一个值,但我的savedInstanceState返回总是为null:

public static class PlaceholderFragment extends Fragment {

        private boolean active=false;



        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putBoolean("active",active);

        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);



        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            if(savedInstanceState!=null)
            {

                active=savedInstanceState.getBoolean("active");
                if(attivo==true)
                {
                    Toast.makeText(getActivity(),"True",Toast.LENGTH_SHORT).show();
                }
                else
                {
                   Toast.makeText(getActivity(),"False",Toast.LENGTH_SHORT).show();

                }
            }

        }

传入FragmentB我使用:

getFragmentManager().beginTransaction().replace(R.id.container,new PrefFragment()).addToBackStack("back").commit();

4 个答案:

答案 0 :(得分:1)

Bundle savedInstanceState将始终在此特定情况下返回null,因为只有在您的片段被android系统销毁并重新创建时才会传入(例如,当屏幕旋转或您的应用因系统资源不足而被销毁时) )。 savedInstanceState不用于在应用程序中持久存储数据,如果你想这样做,我建议使用SharedPreferences:

private void saveBooleanToPreferences(String key, boolean bool){
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
     SharedPreferences.Editor editor = preferences.edit();
     editor.putBoolean(key, bool);
     editor.apply();
}

private boolean getBooleanFromPreferences(String key){
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean bool = preferences.getBoolean(key, false);
    return bool;
}

因此,您的代码将类似于:

 public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("active",active);
        saveBooleanToPreferences("active",active);
    }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            if(savedInstanceState!=null)
            {

                active=getBooleanFromPreferences("active")
                if(attivo==true)
                {
                    Toast.makeText(getActivity(),"True",Toast.LENGTH_SHORT).show();
                }
                else
                {
                   Toast.makeText(getActivity(),"False",Toast.LENGTH_SHORT).show();

                }
            }

答案 1 :(得分:0)

确保包含Fragments的活动不会覆盖onSaveInstanceState(或至少调用super方法)。

答案 2 :(得分:0)

使用sharedPreferences将数据从片段B传递到片段A.

答案 3 :(得分:0)

错误,如果您在没有处理onBackPressed()的情况下按下,那么您的应用程序将关闭而不保存任何实例,但如果您按回家,则会发生保存。记得在backPressed上处理,删除它的超级