ViperArchitectureExample
ViperArchitectureExample copied to clipboard
Favor an Image
Hey, I found minor problem when marking a Image as favorite. I don't know if you are aware of that and just ignore that issue (I ignore such "issues" most of the time :smile: ). However, the problem is as follows:
- search for an Image in
SearchFragment
part ofMainActivity
- open an image -->
DetailsActivitiy
containing aDetailsFragment
(I'm talking about phone layout) starts and displays the image -
MainActivity
get's destroyed in background (i.e. because of low memory) then SearchFragment'sSerachViewState
get's stored into the Bundle, containing the search result. - Mark the image displayed in
DetailsFragment
as favorite - Press the back button -->
MainActivity
andSearchFragment
gets recreated. Since SearchFragment's Bundle contains aSearchViewState
the ViewState will be restored which contains the data from before favoring the Image. That means that the Image gets displayed NOT as favorite while it should be displayed as favorite since it has been marked as favorite by the user.
You can reproduce that issue by enabling "Don't keep Activities" in the developer settings of your device.
I see 4 solutions for that problem:
- Simply ignore that "issue" since it happens rarely and if there is no sensitive data most of the time the user of your app doesn't notice that "issue" anyway.
- Don't use a ViewState at all. I do that if the displayed data is sensitive.
- Use a
RetainingViewState
. By doing so the view state will never be stored into the Bundle and data will always be reloaded when activity / fragment gets recreated. But in that brings another problem: when the user is inDetailsActivity
and presses the back button then itMainActivity
gets recreated and displays the initial state, but the user would expect that the previous search result gets displayed. A workaround would be to save the latest search string into fragments bundle and then rerun the search query. - Rethink the internal "update" mechanism, i.e. use
onActivityResult()
to notify theSearchFragment
that the Image has been marked as favorite.
Hi, thanks for your help. Yes I thought about it for a minute before. Well, for the simplicity and clarity of this project (example of an architecture) I will say that it's a minor issue. But for production application it has to be rethought. I like your 4th advice, or something similar.