Here is my code that is not working:
// Sending bundle this way:
String topUser = String.valueOf(scores.get(arg2));
Bundle data = new Bundle();
data.putString("userprofile", topUser);
FragmentTransaction t = getActivity().getSupportFragmentManager()
.beginTransaction();
SherlockListFragment mFrag = new ProfileFragment();
mFrag.setArguments(data);
t.replace(R.id.main_frag, mFrag);
t.commit();
// Retrieving this way:
Bundle extras = getActivity().getIntent().getExtras();
userName = extras.getString("userprofile");
Basically, the data isn't received. Am I on the right track or is there a better way of doing this?
You should be using the getArguments() method of the Fragment class. So put something like the following inside your Fragment:
Bundle extras = getArguments();
Reference: http://developer.android.com/reference/android/app/Fragment.html#getArguments()
Related
I am trying to send value as a parameter from one fragment to another using bundle but bundle is not is not putting in bundle. i tried to get value in next fragment but value was 0 then i debugged and got that value is not saving in bundle. screenshot is attached.
Here is code
switch(v.getId())
{
case R.id.store_gift_promo:
{
Fragment fragment = new GiftPromotion();
Bundle bundle = new Bundle();
bundle.putLong("id",id);
fragment.setArguments(bundle);
FragmentManager fragmentManager = ((FragmentActivity)mActivity).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
break;
}
}
Please help me solve the problem.
Try it in another way like this:
GiftPromotion gp = GiftPromotion.newInstance(id);
in GiftPromotion:
public static GiftPromotion newInstance(long id) {
GiftPromotion f = new GiftPromotion ();
Bundle args = new Bundle();
args.putLong("longValue", id);
f.setArguments(args);
return f;
}
while retrieving in onCreate of fragment:
long lId = getArguments().getLong("longValue", 0L);
In my Xamarin android app I have a two fragments A & B, A sends a value of string name to Fragment B like below,
mFragmentA = new FragmentA();
string name = "john";
var fragmenttransaction = FragmentManager.BeginTransaction();
fragmenttransaction.Replace(Resource.Id.FragmentContainer, mFragmentA, "mFragmentA");
fragmenttransaction.AddToBackStack(null);
fragmenttransaction.Commit();
Bundle bundle = new Bundle();
bundle.PutString("namekey", name);
mFragmentA.Arguments = bundle;
return mFragmentA;
In fragment B in the oncreateView I try to retrieve it as below,
Bundle bundle = this.Arguments;
var name= bundle.GetString("namekey", "Default Value"); //NULL REFERENCE EXCEPTION
Am getting a null reference exception on the name variable. Why? How do i get the string name in fragment A and assign it to name variable in Fragment B?
Cause you first replace fragments, and then put bundle inside a fragment, which is already on screen, here you are #jobbert xD
Change your code as below
mFragmentA = new FragmentA();
string name = "john";
var fragmenttransaction = FragmentManager.BeginTransaction();
Bundle bundle = new Bundle();
bundle.PutString("namekey", name);
mFragmentA.setArguments(bundle);
fragmenttransaction.Replace(Resource.Id.FragmentContainer, mFragmentA, "mFragmentA");
fragmenttransaction.AddToBackStack(null);
fragmenttransaction.Commit();
return mFragmentA;
Try this example. Before you commit fragment transaction. And after putting all your data to fragmentB object you should replace it with fragmentA.
Bundle bundle = new Bundle();
bundle.PutString("namekey", name);
fragmentB.setArguments(bundle);
Why not just use the constructor or properties?
string name = "john";
mFragmentA = new FragmentA(name);
var fragmenttransaction = FragmentManager.BeginTransaction();
fragmenttransaction.Replace(Resource.Id.FragmentContainer, mFragmentA, "mFragmentA");
fragmenttransaction.AddToBackStack(null);
fragmenttransaction.Commit();
return mFragmentA;
The constructor will execute before the OnCreateView and OnCreate methods execute.
NOTE: In your code, there is no FragmentB. So this is really just passing data from the current Activity/Fragment to FragmentA(since that's what's getting instantiated).
I have two fragments: in the first fragment I do it:
ritorno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
manager.beginTransaction().replace(R.id.content_frame,new SearchFlight()).addToBackStack(null).commit();
}
});
and this works well. In the second I need to return to the first fragment, so I decide to do this:
String s = element.getText().toString(); //I need to return it
FragmentManager manager = getActivity().getSupportFragmentManager();
SearchFragment fragment = new SearchFragment();
manager.popBackStack();
I need to return to the first fragment this String. How could I do it?
Thanks
There are 3 ways
Static Variable : Define public static variable in first fragment, update its value in second fragment.
BroadcastReceiver
EventBus you can even find EventBus tutorial here
pass string to BR or EventBus and you can fetch it in first fragment by defining appropriate method or listener.
Try doing it with a bundle object.
String s = element.getText().toString(); //item to be returned
FragmentManager manager = getActivity().getSupportFragmentManager();
SearchFragment fragment = new SearchFragment();
Bundle bundle = new Bundle();
bundle.putString("key", "value");
fragment.setArguments(bundle);
manager.popBackStack();
And then, in your SearchFragment resolve the bundle like:
Bundle bundle = this.getArguments();
bundle.getString("key");
Do the resolution part of the bundle in your onCreateView of the SearchFragment. Do place a null check on the resolution part of the bundle. If the bundle is not resolved, then the getString("key") method will give a null pointer exception.
I'm trying to send a Parcelable array from MainActivity to one of its fragment using bundle. My code is
Bundle bundle = new Bundle();
MyFragment fragment = new MyFragment();
bundle.putParcelableArray(key, MyParcelableArray);
fragment.setArguments(bundle);
When debugged, I found that MyParcelableArray is not null but bundle mParcelledData is null. Thus, it is throwing a null pointer error.
Why is this happening?
I'm retrieving code in the fragments onCreateView as below.
Bundle bundle = getArguments();
Parcelable[] parcelables = bundle.getParcelableArray(MainActivity.key);
getting null pointer in the above line.
While opening a fragment use below code
Bundle bundle= new Bundle();
bundle.putSerializable("data", MyParcelableArray);
ListFrag newFragment = new ListFrag ();
newFragment.setArguments(bundle);
While obtaining that bundle data in fragment use below code
Bundle b = getArguments();
ArrayList<collection> yourArrayList = (ArrayList<collection>) b.getSerializable("data");
I have the following lines in my code
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, fragment, fargmentTag);
Now I want to add a bundle to my fragment. How can I do this ?
Try this:
Anywhere inside your FragmentActivity class, put this:
MyFragmentClass mFrag = new MyFragmentClass();
Bundle bundle = new Bundle();
bundle.putString("DocNum", docNum); //parameters are (key, value).
mFrag.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.page_fragments, mFrag).commit();
I’m using “import android.support.v4.app.FragmentActivity;” so I use “getSupportFragmentManager()”.
So to summarize the code above, u created a bundle instance and an instance of your fragment. Then you associated the two objects with “mFrag.setArguments(bundle)”. So now the “bundle” is associated with that instance of your MyFragmentClass. So anywhere in your MyFragmentClass, retrieve the bundle by calling:
Bundle bundle = getArguments();
String mDocNum = bundle.getString("DocNum");
Before ft.replace(R.id.fragment_content, fragment, fargmentTag); add the following line:
fragment.setArguments(bundle).
Before calling replace just add fragment.setArguments(bundle)