I have create Tabbed activity with two tab
fragment a Contain edittext and fragment b Contain listview ,and I want to send data from fragment a to fragment b ,Please add the code in full,
please help me.
Note : I do not want pass data to textview Iwant to listview
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
Follow this link
Related
Activity A wants to update a listView inside the same activity.
Value is received from a surfaceview - scan, it should not update the listView until the finish button is pressed.
you can use fragments in your activity and then use fragment manager to navigate between your camera page and the listView page.
you can use bundles to share data between these two fragments.
use this to learn more about fragments:
https://developer.android.com/guide/components/fragments
and for sharing data between fragments visit this:
How to pass variables with Android fragmentmanager
how to send data from a fragment that is part of a viewpager, to another fragment that is also in a viewpager. The problem is when in fragment 1 by a spinner select a data, this data has to go to fragment B.
I solved it using RxBus2. Use the Warrenth library. I leave the link to your repoitorio https://github.com/warrenth/RxBus2
I want to separate logic fragment from activity but the problem is I make api call and save data in fragment. And when user click a item in fragment. I need to send parcelable data to other fragment to show detail info about item.
Is launching fragment in fragment anti pattern for android ?
I would like to hear some opinion about this matter.
Yes, is totally an anti-pattern, remember that you need to see the Activity as a container and fragments as independent sub-screens, so is the Activity responsibility to manage the fragments. I.e.: If you have a Post activity you can have a PostText fragment, a PostImage fragment and all of that is manage by the activity, every fragment is attached to an Activity.
It is not a common practice to have a nested fragment inside a fragment even it can be done. However, it would be better to have an activity as the centric container for all your fragments. You can use EventBus (GreenRobot / Otto) to separate the concerns and do all the API calls in another class and send the results by subscribing to this event.
So I currently have an app that has 4 tabs (fragments). They are fragments A,B,C,D, in that order.
Fragment A is the first view opened (along with B because viewPager loads the view before and after the current view).
When I click a button in Fragment A, it sends Data back to MainActivity and then sends that data out to Fragments B and C.
However, this is where the issue comes into play. Since Fragment B was already called, the View isn't updated once I click the button and send the data over, but Fragment C is because the view wasn't called before.
Is there any way that I can remedy this?
You can do it a few ways right.
Just set the data to the fragment and have it update its views
Have all the fragments like B and C register themselves to recieve data from the MainActivity and when MainActivity gets it's data set you tell all the registered receivers of the new data
Recreate the fragment
Use an event bus and tell all subsribers of the new data and MainActivity, Fragment B would get notified of new data. Fragment C would get its data when created by MainActivity
I think this list is pretty endless tbh
The key here is the fragments need to fetch the data from the actvitiy aswell as be updated by the activity. In which case you need to break your UI update behaviour out of onCreateView and into its own updateUI() function. updateUI(MyData) can then be called from onCreateView and also called in a setMyData() on the fragment. Just make sure you check the isAdded flag in setMyData.
This pretty much says it all:
http://developer.android.com/training/basics/fragments/communicating.html
I used a simple fragment communicator that allows the activity to call the fragment, and the same for a fragment to talk to the activity.
You can change the views with the new data based on calling the method from within the activity. The way I do it is set the fragments in the activity then pass them into the page adapter this way I can call the methods within the fragment and implement the fragmentcommunicator interface on the fragments.
You can honestly even avoid the interface if you want, but if you are going to include the same method in all the fragments to talk to them it is easiest.
If you show code, I can show you a quick example.
I have three tabs.. Personal info,profile info and FinalStep...
First of all i need to move to another tab using a button in one tab activity..
how to do it?
Secondly how to save data in these tabs... as i have a FinalStep tab which contains the final registration button...so i need to obtain data from the other tabs also(personal and profile)
How to do it?
I always save data as a public variable in the TabHost activity. You can access it via getParent().
You simple TabActivity-TabView combination for implementing this. While doing you will get a structure with 4 classes:
1) TabHostActivity: this will host your tabview.
2) Tab1Activity: this will be the view of first tab.
3) Tab2Activity and 4) Tab3Activity similarly will hold the view of tab2 and tab3.
Now for going to one activity to other use can use the TabHost variable used in TabHostActivity and set its currentTab function.
HelloTabWidget.tabHost.setCurrentTab(2);
And yes for saving the data, you can use public variables in TabHostActivity and use it as per your requirements.
For more details on how to use tabview, go to this link:
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
PS: this is a general idea of doing and yes, you can optimize it more as per your needs and requirements and this may not be the best method of doing this.