i want to send multiple bundles from activity to fragment, the problem i'm facing here is bundle 2 get the reference of bundle 1, how to differentiate both the bundles. please provide me some suggestion.
Here is what i pass from activity to fragment,
FeatureTab featureTab = new FeatureTab();
featureTab.setArguments(bundle_DescriptioneTab);
featureTab.setArguments(bundle_User_Review);
fragmentTransaction.replace(R.id.tabcontainer, featureTab, "FeatureTab");
fragmentTransaction.commit();
Here is what i used in fragments to get the bundle,
Bundle 1 :
private void setDescription() {
try {
Bundle bundle = getArguments();
txt_Description.setText(bundle.getString("long_description"));
} catch (NullPointerException e) {
AppUtils.logError(TAG, "NullPointerException");
}
}
Bundle 2:
private void getUserReviewsParcel() {
try {
Bundle bundle = this.getArguments();
UserReviewsParcel userReviewsParcel = bundle.getParcelable("user_reviews");
List<UserReviewsBean> list = userReviewsParcel.getparcelList();
// set the listview adapter
setListviewAdapter(list);
} catch (NullPointerException e) {
AppUtils.logError(TAG, "NullPointerException");
}
}
i'm calling both the methods in onCreateView.
How to send multiple bundles from activity to fragment
Use Bundle.putBundle(KEY,VALUE) to prepare a bundle which contains other bundles and you can access using keys:
Bundle bundle=new Bundle();
bundle.putBundle("bundle_DescriptioneTab",bundle_DescriptioneTab);
bundle.putBundle("bundle_User_Review",bundle_User_Review);
Pass bundle to setArguments method and you can access both Bundle using bundle_DescriptioneTab and bundle_User_Review keys.
Related
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){
.
.
.
}
}
Hi I'm learning now about Fragments and I have some doubts.
V1:
So the correct way to create a new Fragment should be with "newInstance" instead of a constructor and would be something like this:
Fragment fragment = MyFragment.newInstance(variable);
And the code on the class "MyFragment" should be this:
private static final String ARG_PARAM1 = "param1";
private int variable;
public MyFragment() {
//Empty constructor
}
New instance will receive the param and will put them on a Bundle and set the argument for the class
public static mi_fragment newInstance(Int param1) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putInt(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
Then after setting it the method onCreate will pick up the argument:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
variable = getArguments().getInt(ARG_PARAM1);
}
V2:
But I still can't see the problem with using the constructor and setting the arguments programatically:
Bundle bundle = new Bundle();
bundle.putInt("key", variable);
Fragment fragment = new MyFragment();
fragment.setArguments(bundle);
Then I pick up the argument on the method onCreate:
public void onCreate(#Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
if(bundle!=null){
variable = getArguments().getInt("key");
}
}
So I decided to search on the internet about this and I found a good answer but I still can't seem to understand it or when you could use it.
the way to pass stuff to your Fragment so that they are available after a Fragment is recreated by Android is to pass a bundle to the setArguments method.
This Bundle will be available even if the Fragment is somehow recreated by Android.
So my conclusion is that you keep the bundle alive and using the newInstance could make you return to the last fragment for example. But probably I'm wrong so I need help.
Question: What's the main difference between both and how do you benefit from newInstance ? An example.
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.
I have a problem with pass data from activity to fragment, it says null pointer exception, but my data is not null.
This is my activity :
#Override
public void onResponse(Call call, Response response) throws IOException {
dataj = response.body().string();
System.out.println(dataj);
Bundle bundle = new Bundle();
bundle.putString("datak", dataj);
FragmentUtama fu = new FragmentUtama();
fu.setArguments(bundle);
Intent load = new Intent(LoginActivity.this, MainActivity.class);
load.putExtra("datajs", dataj);
startActivity(load);
finish();
}
and this is my fragment :
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
String datas = getArguments().getString("datak");
System.out.println(datas);
return inflater.inflate(R.layout.fragment_utama, container, false);
}
Thanks for your help.
You don't pass data to fragment via Intents. Arguments are passed by setting them to the fragment instance before calling Fragment transaction commit.
SomeFragment f = new SomeFragment ();
// Supply data input as an argument.
Bundle args = new Bundle();
args.putInt("some_key", value);
f.setArguments(args);
then you read it in your fragment.
NOTE: setArguments can only be called before the Fragment is attached to the Activity. So if fragment is already attached, it will not transfer any data to the fragment.
Bundle args = getArguments();
int value = args.getInt("some_key", 0);
you may try this,
use findFragmentByTag it will help you to find fragment and send data over it.
FragmentUtama fu =(FragmentUtama)getSupportFragmentManager().findFragmentByTag(FragmentUtama.class.getSimpleName())
Bundle bundle = new Bundle();
bundle.putString("datak", dataj);
fu.setArguments(bundle);
or
you can have one setter method in fragment and set data from activity to fragment
from Activity.
fu.setData(dataj);
In fragment
String data;
public void setData(String data)
{
this.data=data;
}
use data using getter method of fragment
public String getData()
{
return data;
}
You need to pass the data to MainActivity using extras of an intent and then using setArguments to pass it to the fragment that belongs to this activity when replacing/adding that fragment to its container.