How to send data between fragments with SlidingTabLayout - java

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.

Related

How to send data from a dialogue fragment to all fragments within a Viewpager?

I have run into the problem of sending data, a simple int type, from a dialogue fragment to all the fragments within a viewpager. I have an activity that hosts both the dialogue fragment and the viewpager. I know how to send data from the dialogue fragment to the activity using an interface defined in the dialogue fragment and implementing it within the activity itself. I was thinking maybe it had something to do with the onAttach method but i am not sure. i feel i am overlooking a simple solution here. please help! thanks!
You can create a shared View Model containing a LiveData Object(holding your int). When you want to send data from the dialog fragment to the fragments in your view pager you can call the setValue method on the live data object and set up observers in each view pager fragment that are notified when the LiveData Object changes. If this is new to you then I recommend reading the Android Architecture components documentation https://developer.android.com/topic/libraries/architecture/viewmodel https://developer.android.com/topic/libraries/architecture/livedata
I hope that this helps :)
Learn " setTargetFragment() "
Where " startActivityForResult() " establishes a relationship between 2 activities, " setTargetFragment() " defines the caller/called relationship between 2 fragments.

RecyclerView inside PagerAdapter

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)

Passing ArrayList from Fragment to Activity and Using BottomNavigationView

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);

Passing data from fragment to fragment within a ViewPager

I have a ViewPager activity which will contain different fragments
I want to pass data from Fragment 1 to Fragment 2, Fragment 2 to Fragment 3, and so forth
Now I understand there is a way to do it by sending data back to the Activity and then passing it onto the next fragment by making a custom interface in the fragment and implementing them on the activity, but is there a way to do it so that the data is passed directly?
Thanks a bunch!

Accessing Fragment objects from parent Activity

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()

Categories