繁体   English   中英

从活动到碎片均可打包

[英]Parcelable from activity to fragment

我是可打包的新手,我正在尝试将数据从活动(MainActivity)传递到片段(MainFragment),但是我很难做到这一点。

我使用所有(可打包)数据制作了一个类(InfoBean)。 当我从MainActivity发送数据时 ,来自bean.newTheme( 2131296447 )的数据就在那里,但是当我尝试在Fragment中检索时 ,该值为0

有人可以看看我在做什么错吗? 谢谢您的帮助。

发送数据(MainActivity):

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    InfoBean bean = new InfoBean();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  SecureSharedPreferences theme = SecureSharedPreferences.getInstance(this, "MyPrefsFile");

        int newTheme = theme.getInt("themeCustom", 0);
        bean.newTheme = newTheme;

        Bundle bundle = new Bundle();
        bundle.putInt("theme", bean.newTheme); // debug shows value 2131296447 
        MainFragment mf = new MainFragment();
        mf.setArguments(bundle); 
    //
  }
}

检索数据(MainFragment):

public class MainFragment extends Fragment {

  InfoBean bean = new InfoBean();

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

   //
   Bundle bundle = this.getArguments(); // Debugging shows 0!
     if (bundle != null) {
         bean.newTheme = bundle.getInt("theme");
        }

     if (bean.newTheme == 2131296447) { // White Theme
         mCardView1.setBackgroundColor(Color.parseColor("#E8EBED"));
        } else { // Dark Theme
         mCardView1.setBackgroundColor(Color.parseColor("#282929"));
         relLay.setBackgroundColor(Color.parseColor("#1B1C1C"));
        }

        return rootView;
    }
}

InfoBean.class:

public class InfoBean implements Parcelable {

    public int newTheme;
    public int THEME_DARK = R.style.DarkTheme;
    public int THEME_LIGHT = R.style.LightTheme;

@Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.newTheme);
        dest.writeInt(this.THEME_DARK);
        dest.writeInt(this.THEME_LIGHT);

}

    public InfoBean() {
    }

    protected InfoBean(Parcel in) {
        this.newTheme = in.readInt();
        this.THEME_DARK = in.readInt();
        this.THEME_LIGHT = in.readInt();

}

    public static final Parcelable.Creator<InfoBean> CREATOR = new Parcelable.Creator<InfoBean>() {
        @Override
        public InfoBean createFromParcel(Parcel source) {
            return new InfoBean(source);
        }

        @Override
        public InfoBean[] newArray(int size) {
            return new InfoBean[size];
        }
    };
}

更新

由于您将片段嵌入到xml中,因此无法将值传递给片段类。 为此,您需要使其通过Java代码执行并删除该xml。 进行片段交易,然后它将起作用

更新

您应该尝试在片段的onCreate方法中检索值。

@overide
protect void onCreate(bundle onSavedInstance){
   if (savedInstance != null) {
     bean.newTheme = bundle.getInt("theme");
    }
}

尝试这个

 if (bundle != null) {
         bean.newTheme = bundle.getInt("theme");
        }

代替

 if (bundle != null) {
         bean.newTheme = bundle.getParcelable("theme");
        }

在您的活动中,您正在使用.putInt(“ theme” .....),但在片段中,您将调用.getParcelable(“ theme”)。 由于要尝试获取两种不同的数据类型,因此得到0。

如果您在XML中嵌入了片段,则无法在程序中使用setArguments() 最好使用动态片段创建。

android开发者网站上有一个简短的示例可以指导您: http : //developer.android.com/reference/android/app/Fragment.html当您嵌入了片段以及如何使用该片段处理参数时,还有另一种实现。

这里还有另一种资源可以帮助您:

从活动设置片段的参数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM