I'm currently trying to switch my Android application from individual activities to fragments contained in a single tabbed activity, however, I'm running into some snags with figuring out how to pass data between them. I was originally just using intents. However, now that I'm using fragments, I'm currently storing any data I need as a field variable in my tabbed activity (As this answer outlines). I'm getting null pointer exceptions because my tabbed activity is attempting to load my first and second fragment, but my second fragment depends on a EditText value from my first fragment. Is there any way I can load these fragments one at a time, and pass my field data (and load my second fragment) when the user swipes? If there is a way, is this the best way of solving my problem? I'm very open to other suggestions. Thanks guys!!
Asked
Active
Viewed 106 times
1 Answers
1
There is a special dedicated topic for this here http://developer.android.com/training/basics/fragments/communicating.html.
I would declare two interface one in each fragment. Then implement the interface in the activity. On EditText
change in the first fragment send the value to the activity and store the value in the activity in a instance variable. Then on the second fragment retrieve the value in the second activity from the activity.

Panther
- 8,938
- 3
- 23
- 34
-
Great, I think this will work! I'll accept if I can fix one other small concern I have: is there any way to disable swiping _until_ that EditText is filled? – Alexander Kohler Dec 18 '14 at 17:12
-
i can think of this.. override the page change listeners. If the edittext is not filled, program it to swipe back to the first fragment. – Panther Dec 18 '14 at 17:27
-
Sorry I never got back to you! That worked perfectly with a little bit of gymnastics with my viewpager. Thank you:) – Alexander Kohler Dec 20 '14 at 13:05