Best Way To Keep Actual Data In Application
Solution 1:
In order to reuse the Fragment
UI components, you should build each as a completely self-contained, modular component that defines its own layout and behavior. Each Fragment
then can communicate data to the Activity
, and the Activity
can also send data to Fragment
s.
In your case, the Activity
can hold the actual data. When user modifies something in the details Fragment
, the Fragment
should intimate that to the Activity
. The Activity
, then in turn should inform the list Fragment
.
Solution 2:
Actually, you can use the onActivityResult()
approach with fragments. I've seen this pattern a couple of times in different projects, up to you to decide whether it suits you.
Assuming your left fragment is AFragment
and your right fragment is BFragment
, here's the idea.
(1) In AFragment
, set a target fragment prior to commiting a transaction from A to B:
FragmentbFragment=newBFragment();
bFragment.setTargetFragment(this, 42); // `this` is your AFragment instance// transact here
(2) Provide onActivityResult()
:
@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
// your logic here
}
(3) In BFragment, prepare your data in onDetach()
and call onActivityResult()
on your target.
@OverridepublicvoidonDetach() {
super.onDetach();
if (getTargetFragment() != null) {
Intentdata=newIntent();
// pass your data here
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, data);
}
}
This way, whenever BFragment
pops back, your AFragment
has a chance to handle its data.
Solution 3:
The way I deal with this situation in my app is to use a central "repository" of SharedPreferences to store serialized data across multiple screens. Then every time a new Activity (or in your case, Fragment) gets created or resumed, it reaches into the serialized data repo to populate its shared data.
This is how I would imagine this would work in your case. To use your "user" example, I would create a SharedPreferences file to store my list of user data objects. So I would have a class that stores everything I need to know about a user -- their info, their "like" status, etc.
Assuming I have the users in a List somewhere, I would just serialize that with Gson, and save the serialized data into the SharedPreferences I created. Then, each Fragment can just poll the serialized data to get the current state of the user. If some changes are made, each Fragment would save the data back to the same SharedPreferences file so that other Fragments could then use it to instantiate their views.
Here's some pseudo code...
//Some kind of data class for UserclassMyUser {
String name;
String location;
String likeStatus;
//... whatever else
}
Then, assuming you have a List of users somewhere, I would serialize that List and store it in a global SharedPreferences file like this...
//ArrayList<MyUser> users <- defined somewhereGsongson=newGson();
StringusersJson= gson.toJson(users);
SharedPreferencesuserPrefs= getSharedPreferences("users_storage", MODE_PRIVATE);
SharedPreferences.Editoreditor= userPrefs.edit();
editor.putString("users_data_key", usersJson);
editor.apply();
Then when a new Fragment is loaded and it needs to know the current state of the users, it simply opens the SharedPreferences file and gets the current state of everything like this...
SharedPreferencesuserPrefs= getSharedPreferences("users_storage", MODE_PRIVATE);
StringusersJson= userPrefs.getString("users_data_key", null);
TypecollectionType=newTypeToken<ArrayList<MyUser>>(){}.getType();
ArrayList<MyUser> users = gson.fromJson(usersJson, collectionType);
Just to be clear, this approach would be all done in your Activity, and when you were starting a new Fragment, you would simply pass in the data it needs to instantiate correctly. Then, if a Fragment needs to save the data, it would simply call a method of your Activity and pass it the data that needs to be saved, and your Activity would execute the code above to save it.
Post a Comment for "Best Way To Keep Actual Data In Application"