0

I have created an app with two activities as the activities with two separate lists. Let's call them: Activity1 and Activity2. With the home screen of the app called the MainActivity. As soon as the app is launched, the MainActivity is created and notifications appear in the notification bar based on the list item's statuses.

I've had a look at the lifecycle of Android but I can't understand and figure out where to load the lists and when to save the lists. Also, what is the best way to save large list data in Android to internal storage? Json? Csv? File type basically? :)

For example, would I load both lists when the MainActivity is first created, onCreate(). And then save the list of(for example) Activity1 when the user navigates away from Activity1, onPause()?

To reiterate, this is what I need help on: I've had a look at the lifecycle of Android but I can't understand and figure out where to load the lists and when to save the lists. Also, what is the best way to save large list data in Android to internal storage? Json? Csv? File type basically? :)

Thank you for any help! :)

Muhammad Waleed
  • 2,517
  • 4
  • 27
  • 75
Imdad
  • 769
  • 1
  • 11
  • 31
  • You might also consider saving the data in a SQLite database, you can also create a backup routine to then save a copy to internal storage. That gives you pretty good flexibility. – Hank Wilson Jun 22 '16 at 17:46

2 Answers2

1

As you may have already known, onCreate will be call when Activity is instantiated, so all initialization, including populating the Activity with list data loaded from a file or shared preference. In terms of when to save the list data, it totally depends on how you want to modify the data, as data only needs to be saved when it's changed from its original state.

Of course, you can also load data every time onResume is called, and save data again when onPause is called, just to make sure data is synchronized if you have multiple applications modifying the same list data.

There are many different ways to save list data. I would recommend to use SharedPrefernce as it will save you some troubles. Please read this for how to save ArrayList to SharedPreference. If you don't like this, you can always save the data to a file with some standard format (JSON, XML, etc.) and parse the data when reading from and writing to the file.

Community
  • 1
  • 1
solosodium
  • 723
  • 5
  • 15
  • Thank you, but do you also have a link to somewhere to show how the save data file such as XML to internal storage? List data to a file in internal storage? :) Can't seem to find one. – Imdad Jun 22 '16 at 17:44
  • [This](http://stackoverflow.com/questions/28646757/write-and-read-a-json-data-to-internal-storage-android) is for JSON. [This](http://stackoverflow.com/questions/13630844/read-write-to-external-xml-file-in-android) is for XML. – solosodium Jun 22 '16 at 17:47
0

A good entry point to load a data is in "OnResume". It is called every time your activity will be presented to the user.

Saving your data can be placed in "OnPause".

"OnCreate" is only called when the activity will be created, which will happen only once for your MainActivity.

tequila slammer
  • 2,821
  • 1
  • 18
  • 25