my android app main activity contains two fragments.The main activity also contain two buttons-save and cancel.The fragments contain EditTexts and Spinners.i want to take data from these EditTexts and Spinner upon Save button click event(Event occur in parent activity).Is it Possible?
Asked
Active
Viewed 969 times
0
-
possible duplicate of [Passing data between a fragment and its container activity](http://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity) – SachinGutte May 05 '14 at 18:59
-
Actually,when i press save button(from parent activity)i want to take data from the current fragment and want to save it. – user3495439 May 05 '14 at 19:04
1 Answers
1
You are able to get the current fragment by doing getFragmentManager().findFragmentById(R.id.fragmentId)
that will return the current fragment. You can then check its type by calling getClass()
or by checking its instance as such if(getFragmentManager().findFragmentById(R.id.fragmentId) instanceof MyFragment){
//Code goes here
}
You can then type cast it if it is the correct class and call whatever methods you want
MyFragment fragment=(MyFragment)getFragmentManager().findFragmentById(R.id.fragmentId);
fragment.getSomeData();

KennyC
- 593
- 4
- 12