2

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];
        }
    };
}
Simon
  • 1,691
  • 2
  • 13
  • 28

3 Answers3

0

Update

Since you have your fragment embedded in xml, you can't pass values to fragment class. To that you need to make it excute through java code and remove that xml. Make fragment Transaction then it will work

Update

You should try retrieving values in onCreate method of fragment.

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

Try this

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

instead of

 if (bundle != null) {
         bean.newTheme = bundle.getParcelable("theme");
        }
Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
  • thx for replying. If I do that, value is still 0. bean.newTheme = bundle.getInt("theme"); – Simon Apr 02 '16 at 08:19
  • Weird, Bundle bundle = this.getArguments(); gives 0. So nothing there. I don`t see the mistake. – Simon Apr 02 '16 at 08:24
  • Updated my answer. You should overide onCreate method and try to retrieve value from there – Zeeshan Shabbir Apr 02 '16 at 08:25
  • I am sorry, still a no go and I thought parcelables were easy ;) – Simon Apr 02 '16 at 08:40
  • I think problem is with your InfoBean class why does describeContents() returns 0? – Zeeshan Shabbir Apr 02 '16 at 08:45
  • bean.newTheme is indeed 0 in the Fragment but in the MainActivity it has the value (2131296447) I need and I also pass it through (bundle.putInt("theme", bean.newTheme). So I don`t understand. I though I just could retrive it by calling bean.newTheme but I am wrong. – Simon Apr 02 '16 at 08:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108023/discussion-between-zeeshan-shabbir-and-simon). – Zeeshan Shabbir Apr 02 '16 at 08:49
0

In your activity, you are using .putInt("theme" .....) but in the fragment you call .getParcelable("theme"). You're getting 0 because you're attempting to get two different data types.

craya
  • 613
  • 6
  • 18
0

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

Community
  • 1
  • 1
Pooya
  • 6,083
  • 3
  • 23
  • 43