Android Parcel how to write Bundle - java

I want to write a Bundle into a Parcel, I use the Parcel.writeBundle Method.
I can write it without any Problem but if I want to read it with Parcel.readBundle it returns an empty bundle. What am i doing wrong?
Here is my Code:
Parcel p = Parcel.obtain();
p.writeParcelable(device, 0);
Bundle b = new Bundle();
b.putString("serial_number",MainActivity.this.T_Serialnbr);
b.putString("model_number", null);
b.putString("mac_address", device.getAddress());
p.writeBundle(b);
Bundle b1 = p.readBundle(); // empty Bundle

Related

android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference

i wanna pass the data, in the first fragment
Bundle bundle = new Bundle();
progress = DetailProgressFragmentArgs.fromBundle(getArguments()).getProgress();
bundle.putParcelable("progress", progress);
new DetailProgressFragment().setArguments(bundle);
Log.d("testProgress", String.valueOf(progress.getProgress_id()));
fragmentTransaction.replace(R.id.fragment, new DetailProgressFragment()).commit();
fragmentTransaction.addToBackStack(null);
second fragment
Bundle bundle = getArguments();
progress = (Progress) bundle.getParcelable("progress");
Error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference
You created two instance of your fragment. You should create only one like this.
Basically you receive NullPointerException because you put inside replace() a new instance of fragment without bundle data inside
Bundle bundle = new Bundle();
progress = DetailProgressFragmentArgs.fromBundle(getArguments()).getProgress();
bundle.putParcelable("progress", progress);
DetailProgressFragment myFragment = new DetailProgressFragment();
myFragment.setArguments(bundle);
Log.d("testProgress", String.valueOf(progress.getProgress_id()));
fragmentTransaction.replace(R.id.fragment,myFragment).commit();
fragmentTransaction.addToBackStack(null);

Pass data from an Activity to a fragment [duplicate]

This question already has answers here:
Send data from activity to fragment in Android
(22 answers)
Closed 1 year ago.
I am a little bit lost in this, so I already passed data from my LoginActivity to my DashboardActivity. And it works now (thank god). But now, I would love to pass that data to a fragment.
I have this on my DashboardActivity:
Intent get = getIntent();
String userId = get.getStringExtra(LoginActivity.EXTRA_ID);
Bundle bundle = new Bundle();
bundle.putString("userId", userId);
Fragment fragment = new Fragment();
fragment.setArguments(bundle);
I have this on my Fragment:
Bundle bundle = getArguments();
String userId = bundle.getString("userId");
int uid = Integer.parseInt(userId);
Is it like this?
Getting data in Fragment and Activity are not done in the same way.
To send extra to a fragment, You can do it this way.
In your Activity Class
Bundle bundle = new Bundle();
bundle.putString("userId", "value you want to send");
FragmentClass frag = new FragmentClass();
frag.setArguments(bundle);
Then in Fragement onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("userId");
return inflater.inflate(R.layout.fragment, container, false);
}
The issue with your code is you're getting the argument with a wrong id.
Right way
String userId = getArguments().getString("userId");
From activity to fragment you should use bundle as below
Bundle bundle = new Bundle();
bundle.putString(LoginActivity.EXTRA_ID, "Extra value");
Fragment fragment = new Fragment();
fragment.setArguments(bundle);
Then in your Fragment, get the data for example in onCreate() method
Bundle bundle = this.getArguments();
if (bundle != null) {
String extraId = bundle.getString(key, defaultValue);
}

Passing object or Array List from one Fragment to another Fragment using Android Studio

I am trying to pass the object or the arrayList from this fragment to another. I was using the Intent method but I had the same problem which I do not know what parameter should I put in bundle.putParcelable() method.
BlankFragment1 sendData = new BlankFragment1();
final Series s = new Series(itemName.getText().toString(),
Integer.parseInt(itemYear.getText().toString())
,Integer.parseInt(itemNumberofSeas.getText().toString()),
dropDown.getSelectedItem().toString());
Bundle bundle = new Bundle();
bundle.putParcelable(s);
sendData.setArguments(bundle);
Use setArguments to add the data same as putextra in activity.
Bundle bundle = new Bundle();
bundle.putString("latitude", latitude);
bundles.putSerializable("KEY_ARRAYLIST", DIVID);
bundle.putString("longitude", longitude);
bundle.putString("board_id", board_id);
MapFragment mapFragment = new MapFragment();
mapFragment.setArguments(bundle);
And to get data use getArguments same as getExtra in Activity
String latitude = getArguments().getString("latitude")
Arraylist obj= getArguments().getSerializable("KEY_ARRAYLIST");

pass bundle from recycleview adapter to activity

I want to pass a bundle on click of button to new activity.
This is the code for bundle in the class adapter which extends recycleview adapter
private void passBundle (Vendor mItemSelected){
mBundle = new Bundle();
mBundle.putString("VENDOR_ID", mItemSelected.getVENDORID());
mBundle.putString("CAT_ID", "" + mItemSelected.getVEN_TYPE());
mBundle.putString("VENDOR_NAME", "" + mItemSelected.getVENDORNAME());
mBundle.putString("CAT_ID", "" + mItemSelected.getVEN_TYPE());
mBundle.putString("CAT_ID", "" + mItemSelected.getVEN_TYPE());
mBundle.putString("VENDOR_AREA", "" + mItemSelected.getVENDORADDRESS());
Intent in = new Intent(context,Chat_Activity.class);
in.putExtra("Bundle", mBundle);
Context.startActivity(in);
}
and this is the code where i retrieve the bundle data in another activity.
Bundle bundle = new Bundle();
chatVenID = bundle.getString("VENDOR_ID", "");
catID = bundle.getString("CAT_ID", "");
vendorName = bundle.getString("VENDOR_NAME", "");
vendorArea = bundle.getString("VENDOR_AREA","");
Bundle bundle = new Bundle();
In this code, you are creating a new object of Bundle, so you wont' get any values. Change it to
getIntent().getBundleExtra("Bundle")
You can invoke
Bundle bundle = getIntent().getBundleExtra("Bundle");
chatVenID = bundle.getString("VENDOR_ID", "");
in onCreate(Bundle savedInstanceState) method;
new Bunlde() will create a new bundle object, can't get pass value from prev activity.

Passing data through bundle

I want to send data which i do this way :
public static final String Itemmm = "tryIT";
String item3 = (String)arrayAdapter.getItem(position).toString();
ClassicsA class = new Classics();
Bundle bundle2 = new Bundle();
bundle2.putString(ClassicsB.Itemmm, item3);
class.setArguments(bundle2);
And to receive data I do this:
Bundle bundle2 = this.getArguments();
if(bundle2!=null){
String i = bundle2.getString(ClassicsB.Itemmm, "");
testButton.setText(i);
}
However the data is never being sent, when I check the value of testButton it's empty. Can you see what i am doing wrong? When I add item3 between the brackets it just takes the item3 as a string.
BTW this is communication between 2 fragments.
To Send the data, use the following lines of code:
Bundle bundle = new Bundle();
bundle.putString("KEY", "VALUE");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
And in order to recieve a data use this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("KEY");
return inflater.inflate(R.layout.fragment, container, false);
}
Hope this helps.

Categories