I am singing up a user in 2 fragments in android. First fragment will have personal information and second fragment will contain educational information. I am using fragments inside a viewpager. I want to store the whole data in student object. How do I pass student object from first fragment to second fragment?
in your first fragment
Bundle bundle=new Bundle();
bundle.putSerializable("tag", user);
FragmentTwo frag =new FragmentTwo ();
frag.setArguments(bundle);
in your second fragment
User user = (User) getArguments().getSerializable("tag");
As per Android Developer website All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
For more details you can look up http://developer.android.com/training/basics/fragments/communicating.html
Related
i need an approach how to handle this topic:
i have create a Mainactivity (A) and a Tabbed Activity (B) that contains 3 Tabs (Fragment One, Fragment Two and Fragment Tree)
how to send a string from the Mainactivity to the Fragment One without starting the Tabbed Activity.
when i use bundle
let say: in MainActivity
Bundle bundle=new Bundle();
bundle.putString("key", editText.getText().toString());
BlankFragmentTab1 fragment = new BlankFragmentTab1();
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frag,fragment).commit();
and inside Fragment One i get the String with getarguments.getString("key")
i get a nullpointerexception.
the approach works only by the communication between the Fragments and its container Activity.
thank you very much for your help
LieForBananas is correct.
You can not interact with your FragmentOne without calling its host activity.
However you can use SharedPreferences instead of Bundle for retrieving data.
Having Fragment A and B, B is kinda of extended A. The UI data update from B need to store back in A and A's UI makes decision whether to save to database.
So A is put on the backstack first, then at some time it needs B and B is put on the backstack and in the holder with 'replace()'.
Passing data to B could be don by argument.
How to pass data back from B fragment to the A fragment (if A is 'replaced()' by B on the holder and currently not showing or 'existing')?
Bundle args = new Bundle();
args.putSerializable(TAG_MY_CLASS, myClass);
Fragment bFrgmt = new B();
bFrgmt.setArguments(args);
getFragmentManager()
.beginTransaction()
.replace(R.id.holder, bFrgmt, TAG_B_FRAGMENT)
.addToBackStack(TAG_B_FRAGMENT).commit();
From Developers website:
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.
First send data from fragment to activity, then receive this data in activity, then from Activity you can send data to Fragment with intent and to receive in fragment onCreateView method.
You can communicate among fragments with the help of its Activity. You can communicate among activity and fragment using this approach.
I have two Fragments A and B. I am going from fragment A to Fragment B using Following method.
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new B()).addToBackStack(TAG).commit();
Now I want to send data to from fragment B to Fragment A i.e Previous Fragment.
Can you please give me an idea about how to send data to previous Fragment.
Thanks
Check the documentation about Fragments and its communication.
Do you want to pass info from one fragment to another? One fragment shouldn't know about other fragments. Make the fragment B communicate with its activity through an interface and make the activity pass the data to fragment A
You can communicate between fragments through Activity where you are replacing fragments.
Pass data to activity and then call method of that fragment where you want to update data.
2.Other way you can take a variable in Activity and save the data to this variable and when another fragment (Fragment A) is loaded(Resumed) then show data from Activity variable by getting
((HomeActivity)getActivity).variableName;
You create a subclass of Fragment called MyFragment that A, B are instances of it. Then create a static factory method take a MyFragment as param like:
public static MyFragment newInstance(MyFragment a){
MyFragment b = new MyFragment();
MyFragment b.a = a;
return b;
}
b.a is a field of MyFragment. Then you can use reference a to send data.
My parent activity is launching another activity and needs its fragments to update when the results are returned via onActivityResult. Should I simply implement methods in each fragment to pass the data into them, or is there a different way this should be done?
Thanks!
Make your fragment that should receive data implement an interface with a suiting method.
In the activity when the result is returned from onActivityResult, use the getFragmentManager().findFragmentByTag("yourtag") method to find the fragment. Cast it to the interface and call the method is has.
Make sure to check if the fragment actually exists after calling findFragmentByTag.
If the fragment is the one making the startActivityForResult call, the activity gets the first shot at handling the result. This makes sense when you consider the modularity of fragments. Once you implement super.onActivityResult for all unhandled results, the fragment got a shot at handling the result.
To get the result in your fragment make sure you call : startActivityForResult(intent,123); instead of getActivity().startActivityForResult(intent,123); inside your fragment.
I hope this will solve your problem.
Someday I will have to solve your issue or similar. Per my understanding, you'll have to implement an interface between Activity and a Fragment. The link is at Communicating with Other Fragments . The webpage seems clear unlike other Google pages. A snippet of code:
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Almost always, class FragmentTransaction is involved in managing data. Note how Bundle is used to pass data, also to get data from the client side.
Have fun, keep us posted.
I have a SherlockFragmentActivity with two SherlockFragment in ActionBar.
In each SherlockFragment I ask for several values to user like Step 1 and Step 2 task. In Step 2 Fragment, I have a button that calls webMethod in order to do action but for this webMethod I need all values that user introduced in Step 1 & 2 Fragments.
What is the best practice to pass or get the Step 1 fragment values in Step 2 fragment?
Create a Bundle Object and put Values in the bundle and pass that to next fragment using setArguments().
Bundle b = new Bundle();
b.putString("myString","String Value");
b.putInt("myInt",9);
yourFragment.setArguments(b);
Then you retrieve the same in Second Fragment as
Bundle b = getArguments();
String myStringFromFirstFragment = b.getString("myString");
int myIntFromFirstFragment = b.getInt("myInt");
You can pass values between Fragments through your SherlockFragmentActivity. The main idea is to create an interface and make your activity implement this one to pass data from fragmentA to Activity and from this one to your other fragmentB.
Check out Communicating with activity and Creating event callbacks to the activity in Android docs for more detailed information.
Hope this helps.