Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save temporary data on android?

How can I save some temporary data so that when I close the application all the data is gone? I've tried sharedPreferences but it seems that the data is still there when I open up the application once again.

I've heard that you can save data to some cache memory, but I really don't want the data to be gone if the memory gets "filled" when the app is still up and running. Maybe I should go with some global variables? Not that I know how I could get that to work.

My simple app, which is a "game", opens up and closes activities when you go one step further. It's basically a game filled with stupid pictures ^^. Stuff that has been done in one activity should be saved somewhere, So if i go back I can load the data and make it look like it was before the activity was closed. I hope you understand what I'm saying..

Any ideas how I could store some information that is also easy accessible when you need it.

like image 453
user3925710 Avatar asked Aug 13 '14 19:08

user3925710


People also ask

How do you store data temporarily on Android?

Internal cache files There may be conditions when you want to keep the files temporarily rather than persisting the data in the storage. In this case, you must use the cache directory to save the data. Every application has private cache directory that may be used to store and retrieve temporary data.

What is data persistence in Android?

Methods of local data persistence. Android allows to persists application data via the file system. For each application the Android system creates a data/data/[application package] directory. Android supports the following ways of storing data in the local file system: Files - You can create and update files.

What are storage options available in Android?

Android provides many kinds of storage for applications to store their data. These storage places are shared preferences, internal and external storage, SQLite storage, and storage via network connection.


2 Answers

Use global variables.

Before onCreate define variables like:

int i; or String s = "myString"

Then you can access/change them in any function.

Hope I helped :)

like image 119
TehCoder Avatar answered Oct 13 '22 21:10

TehCoder


You could use global variables and then use Intent to pass them from activity to activity. To do that, you use this:

Intent intent = new Intent(getBaseContext(), MYourActivity.class);
intent.putExtra("variableName", value);
startActivity(intent)

and to get it in the next activity

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("new_variable_name");
}
like image 3
user1282637 Avatar answered Oct 13 '22 22:10

user1282637