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.
Related
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().
I am sending one bundle from an adapter and one bundle from main activity and receive both in fragment. I can get the data for first bundle i.e, pass from adapter but for second bundle i.e, from main activity I cannot retreive. How to do this? I have tried so hard but cant make it work. Please help thank you*
// Adapter... sending data to fragment
Bundle bundle=new Bundle();
bundle.putString("contents", sContents[10]);
bundle.putString("details",sDetails[10]);
bundle.putInt("position",getPosition());
bundle.putBundle("bundle",bundle);
fragment.setArguments(bundle);
//Main Activity //sending data to fragment
Bundle passSize=new Bundle();
passSize.putString("selection",selection);
passSize.putBundle("passSize",passSize);
fragment.setArguments(passSize);
//Fragment //Receiving data from adapter and Main Activity
if(bundle!=null) {
if(bundle.getBundle("bundle")!=null) {
String myValue = bundle.getBundle("bundle").getString("details");
position = bundle.getBundle("bundle").getInt("position");
}
}
//Here not working...... Note: the passing data is correct and No error when debug or run.
if(bundle.getBundle("passSize")!=null){
System.out.println("inside bundle");
if(bundle.getBundle("passSize").getString("selection")!=null){
.
.
.
}
}
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.
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 have two activities. And I passed the argument to the target activity by intent:
Bundle bundle = new Bundle();
bundle.putString("ImagePath", path);
Intent intent = new Intent(getActivity(), DetailActivity.class);
intent.putExtra("paths", bundle);
startActivity(intent);
The target activity DetailActivity has a fragment, and I want to get the argument ImagePath in it. Now I have two method:
I get the argument in the DetailActivity by getIntent() and then pass it to the fragment using setArgmunets()
I get the argument in the target fragment using getActivity().getIntent() directly.
I like the method 2 and use it now because the clean code. But the Android Studio tell me the message Method invocation 'getIntent' may produce 'java.lang.NullPointerException' in getIntent().
So should I abandon the method 2?
Update: Final, I used the method 1, because of this answer :
From the Fragment documentation:
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
You can check for extras..
Intent intent = getActivity().getIntent();
if(intent.hasExtra("paths")){
// get the data
}else{
// Do something else
}
Use Below Code :
Bundle bundle = new Bundle();
bundle.putString(Constants.BUNDLE_DATA, "From Activity");
Fragment fragment = new Fragment();
fragment.setArguments(bundle);
and in Fragment onCreateView method:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString(Constants.BUNDLE_DATA);
return inflater.inflate(R.layout.fragment, container, false);
}
If you want to send large data ,then create a model and make that model implements Serializable .
No, it's not about the intent. It's because in some point of the time(e.g. fragment has been detached from the activity) getActivity() method can return null. So, correct call will be the next:
if(getActivity != null) getActivity().getIntent()
getActivity() on Fragment may produce null.
So you need to use either any interface for communication between activity and fragment or use bundle by passing in setArgument() in instance of fragment.