If I want to pass data between activities in the same application, a) I can use an Intent or b) use a database (passing the id in the Intent instead of the full data.
But I could also use c) a class with static data structures that both activities can access in a store/fetch fashion.
What are the cons of using (c) if I don't care about persistence of the data on app restart?
            Asked
            
        
        
            Active
            
        
            Viewed 184 times
        
    1
            
            
        
        Jim
        
- 3,845
 - 3
 - 22
 - 47
 
- 
                    1The c option is the example of Repository pattern, and the repository is singleton. – Manuel Mato Mar 18 '20 at 08:18
 - 
                    @ManuelMato: I thought the repository pattern means access to a DB (or remote server DB) – Jim Mar 18 '20 at 08:18
 - 
                    1I use the repository to create a flow data between more datasource and use too a cache datasource, but if you haven´t more datasource, maybe you could do all in the repository class – Manuel Mato Mar 18 '20 at 08:21
 - 
                    @ManuelMato: Your suggestion helps on how to properly design the code. But the question is about potential issues we face by storing the data in static structures during the activities lifecycle – Jim Mar 18 '20 at 08:40
 - 
                    ah ooki, I had not understood the statement, sorry xD – Manuel Mato Mar 18 '20 at 08:44
 
1 Answers
2
            The big cons is that the Android OS can kill your process at any time. When the process will be re-created, Android will re-create all of your activities restoring their state.
Since Android doesn't know about how your static data should be handled, you will lose it and Activity B will be in an inconsistent state.
I'll make an example to be clearer:
- Activity A is launched
 - A button on Activity A is clicked
 - Static data structures are being populated before launching Activity B
 - Activity B is launched and can access the static data
 - Android OS kills automatically your process
 - Activity A and Activity B are restored
 - The static data structures are not populated since they would have been populated when the button on Activity A is clicked
 - Activity B is in an inconsistent state
 
        Giorgio Antonioli
        
- 15,771
 - 10
 - 45
 - 70