I'm developing an android application in which is needed one object which contains an arraylist of objects to be edited.
The idea is each of the objects in the arraylist is to be edited in a different fragment.
The app is using ORM to sore objects in database, so I need each object from this arraylist to be edited in different fragments and when save button is pressed to collect the objects from the fragments, to update the main object and save it.
Now I'm creating the fragments and using setters I'm setting the objects for each fragment in the main activity:
DailyFragment fragment2 = new DailyFragment();
fragment2.setdaySchedule(daySchedule);
fragment2.setmDayIndex(1);
Using this approach in each fragment I have reference to the needed element of the arraylist, so it is not needed anything to be done when the main object is saved.
I need advise if there are better ways this to be achieved?
I would consider moving your data out of the fragments.
Look into using something like dependency injection (dagger) to create places to keep and modify your data.
Ideally your fragments contain little more than what is needed to display information correctly. If you bundle everything into activities/fragments you'll continuously be hampered by the need to communicate between them.
Related
I have one Activity and want to share data between that Activity and fragments. I put data in extra while in a fragment and put also other data in it. That way I Have a shared Bundle across my application. I only see examples of passing a Bundle to an Intent but its also possible to change that data while in another fragment. This does not break with the self-containment of fragments. I dont put them in some method in activity because then you will have to cast the activity. Can anybody tell me its right to do? I know about shared pref but I dont want a file based solution. I know about passing parameters with newInStance but I also need to save data back in fragments. passing parameters is only forward not shared.
Passing data from activity/fragment back & forth using Bundles would have some limitations and issues for instance:
To pass a complex object, you'd need to use a serializable marked with a key.
Keeping shared keys in different parts can lead to runtime
errors or data loss if keys are wrong.
Serialized objects are not recommended, but it can be solved with Parcelables. But maintaining that is not that easy for complex objects check here, and you would need to customize that for different types of objects.
Still you don't share data among different fragments but they're just transported; and need to be transported over and over again when you go to a new part of your app.
Not guaranteed to keep the data if the activity is recreated.
Instead of that you'd use ViewModels through MVVM structure where:
Data can be shared on different levels of lifecycle scopes; this means that if the ViewModel is instantiated through ViewModelProvider in activity; then it can be accessed in any part of the activity, or underlying fragments. And if you want to keep only data shared between any fragment and its underlying fragments; you'd bound the ViewModel instantiation to that fragment instead.
ViewModel is instantiated once in the owner, and accessed in the subordinate fragments with no re-instantiation.
If the activity is re-created, it receives the same ViewModel instance that was created by the owner, and the view's data won't be lost.
When the owner activity/fragment is finished, the framework automatically calls the ViewModel's onCleared() method so that it can clean up the resources.
Here is a code lab that you'd check.
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.
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.
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).
I've got a pretty complicated layout: a master-detail view, where the detail fragment contains a pager with 3 other fragments in it. I'm getting a bit lost ensuring that each fragment has the correct arguments passed to it, especially since some data is loaded via async tasks in the main detail fragment and is then pushed into subfragments.
Add to all of this that many fragments are displayed in activities on phones, and I've got lots of different ways of loading the same fragments.
What would be a good way of ensuring that all of the arguments I need are set for each fragment, by every activity/fragment that uses them?
I was thinking of adding a static 'build' method to the fragments that would return an instance of the fragment with the arguments bundle correctly populated, but this doesn't really work when data is set following asyncTasks.
Can anyone suggest how I can manage the data flow between my fragments & activities?
Taking the static 'build' idea one step further, you could create POJOs that hold the necessary arguments for a given fragment and add methods that can do the POJO <-> Intent conversions.
The master view could create a POJO, popuplate its fields, convert POJO to Intent and pass the intent to the fragments. The fragments then could build their own POJO from the intent.