I used tab,pageviewer and fragment. I want to change my edittext value when user swipe the page .... For example I have two tabs in first tab user input two integer value and on page swipe/change tab i want to set the addition of that two inputs on edittext view.
Asked
Active
Viewed 512 times
2 Answers
1
Two fragments in a activity should not communicate directly with each other. The modular way would be to, send the 2 EditText values to the Activity. Add the 2 values in the activity and send the value to the next fragment.
Update: Set the text at onActivityCreated of the Fragment.
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
{
editText.setText(text);
}
if that is not working also, try with a Handler(), like this,
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
{
new Handler().post(new Runnable() {
@Override
public void run() {
editText.setText(text);
}
});
}

Bob
- 13,447
- 7
- 35
- 45
-
i can pass value on the second fragment but onCreateView i can't set that value on edittext view – ramiz samol May 02 '16 at 11:31
-
Do not set the text at onCreateView. Try setting the text at onActivityCreated() – Bob May 02 '16 at 11:38
-
if its not working at onActivityCreated() also, try with a Handler.post() – Bob May 02 '16 at 11:58
-
How to use onActivityCreated() and Handler.post() use in Fragment – ramiz samol May 03 '16 at 05:32
-
But still i have a one problem. When i set value on second fragment i need to go on first fragment and then i'll set the value on edittext of third fragment. – ramiz samol May 03 '16 at 06:29
-
Sorry. Can you explain a bit more. – Bob May 03 '16 at 06:31
-
i have 3 pages ... In first page i have some value ...... in second page i set some value which should be get on third page But My problem is when i go to second page to third page value is not set in edittext but i have to go to first page and then go to third page then the value is set on edittext... – ramiz samol May 03 '16 at 06:45
-
Are you using ViewPager to have 3 pages? – Bob May 03 '16 at 06:53
-
ViewPager will create and have the previous and next page in memory. When you are in the second page itself, ViewPager will create the Next Page(third page) in its memory. So that swiping to the next page will be seamless. When you go to first page, it will destroy the third page. Upon coming back to the second page, it will again create the third page. – Bob May 03 '16 at 07:01
-
I want to know, how are you sending the data from second page to third page? – Bob May 03 '16 at 07:02
-
i fetch data from previous page using static method of the previous page. Example FirstFragment class have method getNumber(), In second Fragment call the method like this FirstFragment.getNumber();.. – ramiz samol May 03 '16 at 07:03
-
how are you sending the data from second page to third page? – Bob May 03 '16 at 07:08
-
i fetch data from previous page using static method of the previous page. Example FirstFragment class have method getNumber(), In second Fragment call the method like this FirstFragment.getNumber();.. – ramiz samol May 03 '16 at 07:09
-
instead of getNumber(), you should try with setNumber(). When you have the data at the SecondFragment, call ThirdFragment.setNumber() – Bob May 03 '16 at 07:16
-
Still same problem ...... you know how to set the another fragment edittext from anotherfragment..... – ramiz samol May 03 '16 at 07:26
-
Any solution for this problem? – ramiz samol May 03 '16 at 07:54
-
You should keep track of all the fragments in the ViewPager adapter. Then when you want to set the data to another Fragment, get the reference of the Fragment from the adapter, and call a method to set the data. refer here for keeping references of fragments. http://stackoverflow.com/a/15261142/4586742 – Bob May 03 '16 at 08:41
-
So, instead of static methods, you should have a method setNumber() in Fragment3. From Fragment2, get the reference of Fragment3 from the Adapter, and call the method setNumber() – Bob May 03 '16 at 08:42
-
Example In first page i set some value and then go to second page. In Second page button click i want to go back first page and i want to see the previous value (In fragment) – ramiz samol May 05 '16 at 07:01
0
You can use a singleton class and define a variable there, or define a variable in the parent activity, or use shared preferences, or use application context.

ozo
- 763
- 7
- 13
-
I can pass value on the second fragment but onCreateView i can't set that value on edittext view. – ramiz samol May 02 '16 at 08:40