Best way to keep actual data in application

ⅰ亾dé卋堺 提交于 2019-12-13 05:29:59

问题


I Faced some problem with the relevance of the data in application.

In our app we have 1 activity and many fragments.

For example, we have 1 Fragment with list of User and there button for like(with 3 states: none/like/favorite). In next page we have full description of User and like button too. If we press like button in userList, there will be no problem and in details screen we see correct like state. But if we click like button in User details and go back to list, there still past data here.

We can't use activityForResult because fragment.

We using rx, maybe there some way to easy resolve this problem?


回答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 Fragments.

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.




回答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:

Fragment bFragment = new BFragment();
bFragment.setTargetFragment(this, 42); // `this` is your AFragment instance
// transact here

(2) Provide onActivityResult():

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // your logic here
}

(3) In BFragment, prepare your data in onDetach() and call onActivityResult() on your target.

@Override
public void onDetach() {
    super.onDetach();

    if (getTargetFragment() != null) {
        Intent data = new Intent();
        // 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.




回答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 User
class MyUser {
    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 somewhere
Gson gson = new Gson();
String usersJson = gson.toJson(users);
SharedPreferences userPrefs = getSharedPreferences("users_storage", MODE_PRIVATE);
SharedPreferences.Editor editor = 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...

SharedPreferences userPrefs = getSharedPreferences("users_storage", MODE_PRIVATE);
String usersJson = userPrefs.getString("users_data_key", null);

Type collectionType = new TypeToken<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.



来源:https://stackoverflow.com/questions/36582854/best-way-to-keep-actual-data-in-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!