Search code examples
androidandroid-fragmentsbundleparcelable

Parcelable from activity to fragment


I am new to parcelable and I am trying to pass data from an Activity (MainActivity) to a fragment (MainFragment) but I`m struggle to get this right.

I made a class (InfoBean) with all the (parcelable) data. When I send the data from the MainActivity, the data from bean.newTheme (2131296447) is there but as soon as I try to retrieve in the Fragment, the value is 0!

Could someone pls have a look, what I`m doing wrong? Thank you for your help.

Send data (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); 
    //
  }
}

Retrieve data (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];
        }
    };
}

Solution

  • If you have embedded fragment in your XML you can't use the setArguments() like that in your program. It is better to use dynamic fragment creation.

    There is a brief example in android developer website which can guide you: http://developer.android.com/reference/android/app/Fragment.html there is also another implementation when you have embedded fragments and how to process arguments with that.

    There is also another resource here which may help you:

    Set arguments of fragment from activity