Getting data from Firebase to Fragment - java

I have one Tabbed Activity which has 3 Fragments. In every Fragment I need to display some data from my realtime firebase database. So i thought that instead of connecting to database in every Fragment, I would retrieve data to static variable inside my tabbed activity, and then display this data calling this variable from every Fragment. But when I try to set my text to data from Firebase it shows an error, because in that moment, my static variable is "null". How do I make sure that that first I retrieve the data to my variable and then set my text. Because right now, my tabbed activity has ValueEventListener inside the onCreate method, and I am trying to set the TextView, from OnCreateView inside Fragments. I made some tests and realized, that the onCreate method inside of Tabbed Activity is called first, but onCreateView from my Fragment is called shortly after when the data isn't retrieved yet.

You can simply get Reference of your fragments like below
ExampleFragment myFragment = (ExampleFragment)getSupportFragmentManager().findFragmentByTag(ExampleFragment.TAG);
And then when the data finished loading. Send it like below code.
myfragment.sendDataToFragment(myData);
i hope it helps.
Also you can use interfaces to communicate between Activity and fragments.

Related

how can i notify the adapter that data has been changed/deleted in THIS scenario

suppose i have an app that has a tool bar and a fragment container in the main activity, the fragment container in this situation is displaying a fragment that has a recyclerview which has a list of objects retrieved from a basic room database, and the toolbar has a button that clears out all the data stored in the database (which means the code that responsible for this is located in the main activity not in the fragment), how should i go about notifying the adapter inside the fragment that the object list has been updated?
i don't think any code is necessary here as it's more of a general question, but i will gladly share all the code necessary if its needed.
You can make the adapter a public static variable and call notifyDataSetChanged from the main activity

How to transfer the activity and fragment information to the server with a button in the activity?

I have an activity that contains several edit texts and a view pager
And inside the view pager there are some text edits
Now, I want when the user fills the form inside the activity and the form inside the fragment, all the information will be transferred to the server by prenter image description hereessing one button.
The problem is that the button is in the main activity and I don't have access to edit the texts inside the fragment
What should I do ???
As your Activity is in control of the Fragment, you could just implement a custom method inside the Fragment like fun getFormInput(): Map<String, String> or similar.
When the "send" button is clicked, you use that method to receive all information from the Fragment.
Alternatively, you could implement a custom listener, with the observer pattern, to be notified inside the Activity on every change of a form field in the Fragment.
You can add an interface that will return you the map object as mentioned above.
Create a method in the interface that returns the data stored in all the dit texts in any Data Structure that best fits your use case.
In your fragment - add a watcher to all your edit texts and send the values present in the edittexts to the activity using the method in the interface.

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)

Proper use of instance state save from fragments

I have a fragment an onCreateView I do a background asyn call to fetch some data to display.
I save the object that is the result of the background call as a member variable of the fragment.
When I see the UI and the data all is ok and when I press the home button the app goes to background. When I reopen the app the data are in the UI inflated by my fragment.
So now I am not sure about the following:
1) Should I save my object in the onSaveInstanceState?
2) Where should I expect to restore it? In the bundle passed in the onCreateView?
3) When would the data be saved in my bundle? I mean how can I see that without saving it right now, I would lose the data from my UI?
In your fragment constructor, add the following line:
setRetainInstance(true);
Control whether a fragment instance is retained across Activity
re-creation (such as from a configuration change).
Also like an activity, you can retain the state of a fragment using a
Bundle, in case the activity's process is killed and you need to
restore the fragment state when the activity is recreated. You can
save the state during the fragment's onSaveInstanceState() callback
and restore it during either onCreate(), onCreateView(), or
onActivityCreated(). For more information about saving state, see the
Activities document.
It seems that your fragment works alright by fetching the data every time it's shown.
If you want to save its data to avoid the async fetching you can use onSaveInstanceState, which requires you to save your data using a Bundle.
Then in onCreateView check if savedInstanceState is null and if it is do the async fetch, otherwise recreate you data from the savedInstanceState Bundle.
It depends on your data (size).
Do you want to have a fragment on the back stack (with retained fragment you cannot)?
For "home button action" and non-retained Fragment implement Parcelable interface and use onSaveInstanceState() to save and onCreateView()/onCreate() to restore data from a Bundle.
Be aware for back button you will loose your data.

How to send data between fragments with SlidingTabLayout

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.

Categories