Adding a bundle to a Fragment from a FragmentActivity - android

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)

Related

Passing Value from One Fragment To Another returns null

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).

How to get element back on popBackStack( )

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.

Pass data between two fragments in same activity and update WebView

Hello I'm trying to pass a String value from a fragment to another one. I have a single Activity.
The thing that I'm trying to do is when a listview item is pressed send the value to the another fragment, load the fragment (This I had made with this code):
Fragment cambiarFragment = new FragmentInterurbanos();
Bundle args = new Bundle();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction
.replace(R.id.container, cambiarFragment)
.addToBackStack(null)
.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
// Commit the transaction
transaction.commit();
And then retrieve the String and update a WebView placed in the new fragment (This method cannot be launched when the user select in NavigationDrawer this section (Fragment).
Thanks for your help in advance!
There are many ways you can achieve this... you could pass the string to the new Fragment through a Bundle like this:
Bundle bundle = new Bundle();
bundle.putString(key, value);
fragment.setArguments(bundle);
or maybe have a DataController class where you store the string and the new fragment retrieves it.

Sending Bundle with Fragment Transaction

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()

Android: Passing a non-primitive ArrayList type from one fragment to another in a bundle

I currently have this Custom ArrayList:
ArrayList<PlaceDetails> place_list = new ArrayList<PlaceDetails>();
which will be populated during the onCreateView() portion.
I am unsure as of how do I pass this ArrayList in a bundle from this fragment class to another fragment class. Below is the snippet of my codes:
public void Map(View view){
if(hasConnection() == true){
Bundle b = new Bundle();
// how should I be passing the ArrayList in this bundle?
FragmentTransaction ft = getSherlockActivity().getSupportFragmentManager().beginTransaction();
TOnlineMapViewFragment mapfrag = TOnlineMapViewFragment.newInstance(b);
ft.replace(R.id.container, mapfrag).addToBackStack(null).commit();
}
}
So I've created the bundle and I wanted to pass it to the next fragment with the newInstance() method. How should I do this?
Consider implementing Parcelable interface in your classes. Then you would be able to store PlaceDetails in Bundle and pass it to setArguments() method.
I've found a tutorial: http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/
I haven't testes it but it looks good.
Are you really sure a bundle is really needed?
TOnlineMapViewFragment mapfrag = TOnlineMapViewFragment.newInstance(b);
mapfrag.setPlaceList(place_list)
Should be trivial to create setPlaceList(ArrayList<PlaceDetails> place_list) ...

Categories

Resources