Kotlin sending data to fragments - android

I am making an app that pulls from a mySQL database. I have a main activity which holds a drawer with a bunch of fragments. If I wanted to make a query and save information in main activity in an array or something, would it be accessible in all of my fragments? I've had trouble trying to send data from fragments, and just read that fragments in the same activity all share the same data. Could someone confirm that if I made an array in mainActivity, that the array would be accessable and mutable by the fragments? (this is my first post, sorry if it's bad)

Why don't you move the array from main activity and create repo which will provide data on request for fragments and main activity. Change in orientation can cause the data in main activity to be wiped out if you are not handling the orientation.
The best way I would recommend is to use view model for the main activity and use that for fragments.
Share data between fragments
Hope it helped. Happy coding :)

Related

How to restore Activity with my current Listview Data while switching between activities?

Hello Everyone,
I had do a search on activity retain state.So I am getting to options/solutions for that:-
Using savedInstanceState() and retainInstanceState() methods.
Using Parent Child hierarchy declare in manifest and its working.
(Example Url:-How can I return to a parent activity correctly? I had same problem mention in Note scetion of the answer.That case is match with my problem.
So,I want to retain the activity states just like Whats app(call/chats/contacts fragments).
But In My scenario,I am fetching the contacts from server.So how can I persist my data while switching between chat and my fragment activity?
So while timing to fetch new data of my contact list from server.I want to save ui of my listview/recyclerview with old data previously I had.
Also suggest which method is good from above two methods or need to implement in other way.

Correct architecture when accessing data from many different fragments

I am working on a Android app which have 5 fragments and some java classes.
I have to be able to read and edit an arraylist containing pojo's from across these fragments and classes. For example updating from the internet and then updating recyclerView in one of the fragments or sorting the objects in a recyclerView in one fragment and have those changes updated in the recyclerView in another fragment.
I have been looking at notifyDatasetChanged, but cannot get it right, when starting an update in the background and then wants it to update onSucceed in the active fragment.
I have been looking on RxJava with the Arraylist as observable, but once again I ran into problems when I wanted to subscribe from multiple fragments.
And of course I did a arraylist in a singleton, but I am pretty sure that is bad coding :-)
I would put the data that is going to be accessed by all of the fragments in a Service. Each Fragment can bind to the service to retrieve a reference to the data and to register a listener (you will have to make a custom one to handle the events that you are interested in) that will tell each Fragment to update its own view. Each Fragment would implement its own Adapter that would wrap the shared data that lives in the Service.

Communication between running Fragment and Activity

We are using Retrofit and Activeandroid for my project.
Currently we are facing an issue.
The pattern which we follow in our project is, We get data from server and save it into local database and after data saved we call routine which fetches data from database and populate UI this all happens in single Activity..
Now we have an activity which makes 3 server request, and due to which the amount of code in activity increased.
We are trying to reduce Activity code by creating fragment for the activity and giving responsibility of fetching data and displaying data to Fragment. Rest call will be made by activity. Now once the data is loaded from all the 3 request we need to inform fragment about data is loaded, what is the best way for this.
And is it even possible to send data to fragment once it is loaded.. or the approach we are following is not correct..
Please guide us on this..
Edit1
I read about EventBus. Can event bus solve this problem or it will effect the efficiency.
If you are storing those Fragment instances in your Activity then it will be much easier.
1) Create loadNewdata(DataType data) method in your Fragment.
2) Pass the data into the Fragment after getting response from server in your Activity
((YourFragment)fragment).loadNewdata(yourData);
I would suggest to use Otto.
By this you do not need to store your data in local db(if you don't want it).Just simply post your responce whenever you received from rest API. As Otto also provides the functionality of sharing custom objects between fragments/Activity. It also helps you to make your code modular.
You will find a working example here & here.
You can use Interfaces for this and send data to activity.when response come back pass reference of to show which fragment data needs to be populate the UI.

better approach for using viewpager with fragments having listviews being populated from webservice call?

First I explain the structure of my app. To be precise MainActivity of my app contains Viewpager with two fragments. Each fragment contains a listview. The data inside listview is being populated from web service call.
This web service call is actually done in activity not in the fragments through AsyncTaskLoader which loads data for both listviews and saves the response in two Application variables (Lists of data for both listviews). On finish of loader , i just do viewpager.notifydatasetchanged() (which reloads the fragments , thus sets the data in listview).
As the fragments in viewpager are initialiased by viewpager's adapter no by the activity directly, I found this approach better.
Options I had, but rejected (pls correct me If I could utilise them)
Asynctask to call webservice (headache if handling rotations)
IntentService (updation of UI)
Is there any other approach to load data and handles the fragments?

Placing AsyncTask in Activity or Fragment?

I am trying to design an Android app which:
grabs JSON data from an HTTP link
iterates through the data and forms an ArrayList of my Object.
Now the HomeActivity extends an ActionBarActivity, which implements TabListener.
It has 2 tabs with a Fragment in each. Fragment 1 is going to hold a listView from the JSON data. Fragment 2 is going to show a Google Map with markers based on the same JSON data.
Now, I'm just wondering what is the best approach to use this AsyncTask.
Should I place it in the Activity and then use interfaces to pass that ArrayList to both the Fragments?
Or ... how should I do this? Thanks! Some tips on caching would also help.
Yes, if your fragments are both using the data, it seems reasonable to put your AsyncTask in the activity. If the AsyncTask is a non-static inner class of the Activity, then in your postExecute you can fetch pointers to your fragments from your activity, and then call methods on the fragments to give them the new data (or tell them that you have stored it somewhere -- however you've implemented it).

Categories

Resources