Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving data upon closing app and retrieving that data

I know, there are plenty of questions in regards to saving/retrieving data on here. I was doing find looking things up on my own and really thought I could manage to find my answers without having to "ask a question", but I began to wonder something that I haven't seen an answer for on here.

MY SITUATION:

Naturally, I'm making an app. Upon closing the app, I want to save a simple array of numbers (0 or 1) or boolean values as it were. Upon starting the app, I want to search for that array, if it exists, and retrieve it for use within the app.

I began placing my code into the activity in which the array would be used. But, I started wondering if I would have to copy/paste the overridden onStop() function into all of my activities? Or do I do it in the main activity and somehow link the other activities.

Basically, no matter what state/activity the app is currently on when the app is closed, I want to be able to save the array of int/bool and open it back up when the app is started.

Maybe I didn't know how to search for what I wanted, so explaining it felt like the right thing to do.

I don't mind doing more searching, but if someone would point me in the right direction at the very least, I'd be extremely grateful.

EDIT: If there's a better way to do what I want than what I described (i.e. using a different state instead of onStop(), for instance), please feel free to throw out ideas. This is my first time actually having to deal with the activities' lifecycles and I'm a bit confused even after looking through the android development tutorials. I really think they're poorly done in most cases.

like image 614
lilgodwin Avatar asked Mar 26 '15 17:03

lilgodwin


People also ask

Where can I store my app data?

Every application in the device has some private storage in the internal memory and you can find this in android/data/your_package_name directory. Apart from this internal storage, the rest of the storage is called the Shared Storage i.e. every application with the storage permission can access this part of the memory.


2 Answers

When you application needs to save some persistent data you should always do it in onPause() method and rather than onStop(). Because if android OS kills your process then onStop() and onDestroy() methods are never called. Similarly retrieve data in onResume() method.

like image 60
Aniket Thakur Avatar answered Oct 19 '22 06:10

Aniket Thakur


Looking at the purpose you want to fulfill, SharedPreferences is all you want.

The documentation states:

"SharePreferences provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed)."

like image 3
Chintan Soni Avatar answered Oct 19 '22 05:10

Chintan Soni