I have an ActionBarActivity that has a ViewPager with two Fragments and tabs created with ActionBar from the support library. My ActionBarActivity has also two buttons: cancel and save. When the save button is pressed, I need to get data from both of my Fragments. How do I do this? This data is stored fetched from the layout and stored in local variables in the Fragments.
You should be able get access to your fragments by use of FragmentManager by id or tag. getFragmentManager().findFragmentById() or getFragmentManager().findFragmentByTag()
Related
how I can change a TextView in an android fragment by variable of MainActivity. I try to log geographical values (from -> to in miles) and want this result show in a fragment live.
My App has a Viewpager and three fragments. In my MainActivity I have a FloatingAction Button for start tracking and stop tracking. The result will be save in a variable on MainActivity. The fragment is loaded already.
I think I need a construct like listener for my fragment?
I have a main fragment that contains a viewpager. This viewpager gets the same secondary fragment (different from the main one) but with different parameters every time. Inside the said secondary fragment that is inside the viewpager, i have a recyclerview. Inside these recyclerviews are some fields that the user fills up. When the main fragment (the one that holds the viewpager) is closed (via a button click) i need to get the data from each recyclerview. How do i to that?
The best way to solve this problem is that implement interface which will give call back to activity holding these fragment and then from Activity pass it to fragment(main) and then use viewpager to get Fragment(secondry) and pass data.
Steps one would be implent interface which will give callback to activity and the get fragment from viewpager(Link for same)
I am having the following problem, my application has two fragments, one with a Spinner where it contains the contents of the ArrayList and another fragment containing a ListView, I want to pass the contents of the Spinner ArrayList to the Activity to send it to the other fragment, I am doing so because I'm using a BottomNavigationView. Any idea how to do it?
your Fragments have context. This context could be casted to your Activity.
in Fragment
((YourActivity) getContext()).send(contents);
i am using a tabHost for 4 fragments in this fragments i am calling custom adapters for each fragment when i move from one fragment to another fragment my list view is reloading freshly i dont want to refresh my listview from moving one fragment to another
Help me guys
Make your data object the member of your activity that is initializing the fragments.
Check this out. I would recommend using a ViewPager with a tabbed indicator, or a set of custom buttons linked to the pager. I've implemented this solution in several apps where each fragment contains heavy content, and the performance was great. Create all your fragments, and setup the pager on each change, this way the content of your "tabs" will stay the same on user switching.
Basically I am trying to create an app that passes data filled in an EditText on on one fragment, into a TextView on another fragment on a button click(The buttons is in the first fragment with the EditText). I use the SlidingTabLayout. I have 2 java classes that both extend Fragment and both inflate separate xml layouts(in the onCreateView). I have a java MainActivty with a public class"SectionsPagerAdapter that extends FragmentPagerAdapter, which depending on the swipe of the user displays 1 of the 2 Fragment classes. I am really confused on how I can send data between the 2 fragment sot that from the EditText in 1 fragment can get sent to the TextView in the other fragment on a button click. Could the suggested solutions be explained as simple as possible because I am relatively new to Android Studio. Many thanks in advance.
As per my understanding, basically you want to pass data between two fragments.
You can use activity for that from where fragments are initialized.
you can do this.
in MainActivity.java:
have a function setData(Object obj) and getData() which returns that object.
From fragment:
You can call those function of activity to save your data and get your data.
Here's way:
MainActivity activity = (MainActivity) getActivity();
Object obj = activity.getData();
activity.setData(obj);
I hope it helps.