I am trying to pass arraylist from activity to fragments but i am getting a null value using the parceable.
In Main Activity:
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("cbookings", customerbooking);
Todaybooking tb = new Todaybooking();
tb.setArguments(bundle);
Array list data declarations
ArrayList<Cbooking> customerbooking = new ArrayList<>();
In fragments:
ArrayList<Cbooking> customerbooking = new ArrayList<>();
Bundle extras = getActivity().getIntent().getExtras();
customerbooking = extras.getParcelableArrayList("cbookings");
Log.wtf("test", customerbooking.toString());
You are using setArguments() to pass the data into the fragment. Therefore, you need to use getArguments() to retrieve the data, not getActivity().getIntent().getExtras().
Related
How do you pass LiveData with intent to another Activity?
I'm trying to pass the LiveData object into a new Activity that uses ViewPager 2 to display one object at a time.
Here is Live data in the ViewModel
private LiveData<List<WrestlersEntity>> mWrestlersList;
public LiveData<List<WrestlersEntity>> getWrestlersList() {
return mWrestlersList;
}
Fragment passing live data.
adapter.setOnItemClickListener(wrestler -> {
Bundle bundle = new Bundle();
bundle.putSerializable("Value", (Serializable)mViewModel.getWrestlersList());
Intent addEditIntent = new Intent(getActivity(), AddEditWrestlerActivity.class);
addEditIntent.putExtras(bundle);
startActivityForResult(addEditIntent);
Pager Activity
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
List<WrestlersEntity> wrestler (List<WrestlersEntity>)bundle.getSerializable("Value");
I get the following error.
java.lang.ClassCastException: androidx.room.RoomTrackingLiveData cannot be cast to java.io.Serializable
If you can't cast live data as Serializable what are my other options to pass a LiveData> to a new activity.
here is a link to the git if you want to look at the full code.
https://github.com/Shawn-Nichol/Wrestlers
Your getWrestlersList() method returns a LiveData wrapping your List<WrestlersEntity>. What you want to pass to your Bundle is the List<WrestlersEntity> directly, but wrapped in a Serialized implementation of List. That's why the code below wraps the value of your LiveData in an ArrayList.
So, you can do this instead:
bundle.putSerializable("Value", new ArrayList<>(mViewModel.getWrestlersList().getValue()));
And this to read it back from the Bundle:
List<WrestlersEntity> wrestlers = (List<WrestlersEntity>)bundle.getSerializable("Value");
I am trying to get bundle extras from activity 1 to fragment of activity 2 but getargument() always returns as null.
//Using this to get string in fragment
String value = getArguments().getString("abc");
//activity1 code that i used to get the extras
Bundle bundle = new Bundle();
bundle.putString("abc", townextra);
UserFragment myFrag = new UserFragment();
myFrag.setArguments(bundle);
There is a problem with logic of passing data.
Correct way would be: Pass data from Activity1 to Activity2, and then from Activity2 to Fragment.
all fixed. I added a new function on my activity 2 to create fragment. instead of doing it in oncreat. it all works fine, thanks
Your Activity
Bundle bundle = new Bundle();
bundle.putString("params", "Your String data");
// set MyFragment Arguments
MyFragment myObj = new MyFragment();
myObj.setArguments(bundle);
The Fragment.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam = getArguments().getString("params");
}
}
But will be better if you show the code how you pass data between activities and then use my answer.
And please add your fragment code.
I am passing data from an activity to fragment by using following code.
Bundle bundle= new Bundle();
bundle.putString("seatId", seatId);
fragment.setArguments(bundle);
Then I consume the data by using following code in my fragment.
Bundle args = getArguments();
if(args!=null && args.getString("seatId")!=null){
matchId = args.getString("seatId", "");
}
Now after this I want to set bundle to null.
The issue is once I set data to fragment through bundle, it keeps it there.
Get hold of the bundle or simply getArguments().clear() as stated here.
Below is a snapshot of the activity code. I want to send the lists to separate fragments. The first one works fine, however in stepsFragment.setArguments(stepsBundle); setArguments isn't recognized. thank you.
//
// Send the ingredients array list in Parcelable to the Ingredients Fragment
//
private void sendArrayToIngredientsFragment() {
//Pack Data in a bundle(call the bundle "ingredientsBundle" to differentiate it from the "stepsBundle"
Bundle ingredientsBundle = new Bundle();
ingredientsBundle.putParcelable("Recipes", recipes);
//Pass Over the bundle to the Ingredients Fragment
IngredientsFragment ingredientsFragment = new IngredientsFragment();
ingredientsFragment.setArguments(ingredientsBundle);
getSupportFragmentManager().beginTransaction().replace(R.id.ingredients_fragment_container, ingredientsFragment).commit();
}
/*
Send the steps array list in Parcelable to the Steps Fragment
*/
private void sendArrayToStepsFragment() {
//Pack Data in a bundle(call the bundle "stepsBundle" to differentiate it from the "ingredientsBundle"
Bundle stepsBundle = new Bundle();
stepsBundle.putParcelable("Recipes", recipes);
//Pass Over the bundle to the Steps Fragment
StepsFragment stepsFragment = new StepsFragment();
stepsFragment.setArguments(stepsBundle);
getSupportFragmentManager().beginTransaction().replace(R.id.steps_fragment_container, stepsFragment).commit();
}
}
Take sure, your fragment classes both extends from Fragment class. If that so, there must be method setArguments()
I'm trying to pass a String array from one activity to another but when I try to read the array in the second activity, the values are null.
Here is how i'm passing the array from the first activity:
Bundle bundle = new Bundle();
bundle.putStringArray("Array", createArray(text));
Intent itemIntent = new Intent(this,Details.class);
itemIntent.putExtra("passedArray", bundle);
startActivity(itemIntent);
createArray(text) is a method that returns an array.
Here's how i'm trying to read the array in the second activity:
Bundle extras = this.getIntent().getExtras();
String[] array = extras.getStringArray("Array");
How do I initialise the array in the second activity with the corresponding array values that have been passed to it? If I try and read any of the values they have not been initialised and are null.
Don't use bundle try this and first calculate your array:
itemIntent.putExtra("passedArray", createArray(text));
startActivity(itemIntent);
And receive them as
String[] array = this.getIntent().getStringArray("passedArray");
I think you are messing around with bundle and extras. Look at this question. I think it can help you
Sending arrays with Intent.putExtra
You are nesting the bundle containing your array within another extra.
That means you'd have to get the "passedArray" extra (which is a bundle) and then get your "Array" from the bundle that you just extracted. Instead change your code to this
Intent itemIntent = new Intent(this,Details.class);
itemIntent.putExtra("Array", createArray(text));
startActivity(itemIntent);