5

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3925710
  • 55
  • 1
  • 1
  • 3
  • what is wrong with just regular class variables to hold the data? when the activity is finished the data is garbage collected – tyczj Aug 13 '14 at 19:52
  • Why don't you use fragments? you can add a fragment to the backstack which would probably help your cause, and all the processing can be done in one activity where you could store your temp data, also what data are you trying to store? objects? – Jesson Atherton Aug 13 '14 at 19:59

3 Answers3

4

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 :)

TehCoder
  • 209
  • 1
  • 8
  • 1
    I'm new to Stack Overflow and I'm really wondering why people Down Vote your answer, without explaining why... – TehCoder Aug 13 '14 at 19:56
  • I didn't downvote, but there are no global variables in Java. Moreover, what you are calling a global variable is actually an instance variable - not even a class variable. – Vikram Aug 13 '14 at 20:03
  • Okay. That's what I call them :p Thank you – TehCoder Aug 13 '14 at 20:05
  • Great idea! but when I come back to the activity after a while the variable will have its "instance" value, if you know what I mean. – user3925710 Aug 16 '14 at 10:42
  • then pass it via Bundle – TehCoder Aug 17 '14 at 23:58
  • sigh, somebody increase my reputation above 125, so i can give this answer the downvote it deserves; and to the question too for marking this as accepted. Not only does the solution incorrectly calls instance variables global, but also instance variables cannot be used in other activities or services. Absolutely bad answer. – user2297550 Jun 22 '18 at 07:19
  • Actually this answer is more relevant than others. You can declare as `public static String s = "myString"` in a class and access it in another. Probably, it would not qualify as global though. – Da Man Apr 14 '19 at 07:18
3

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");
}
user1282637
  • 1,827
  • 5
  • 27
  • 56
  • Thanks for the tip! But would that really be good? I mean, I'll probably have a lot of variables from a lot of activities. Passing them all whole the time from activity to activity, wouldn't that be a bit excessive? – user3925710 Aug 16 '14 at 10:28
  • Yeah it could be. One thing you could so is every time the app is STARTED, clear SharedPreferences. This will ensure it is clear every time you start/restart the app. http://stackoverflow.com/questions/3687315/deleting-shared-preferences – user1282637 Aug 17 '14 at 14:10
2

Hi you're approach is right for saving temp data in to the sharedPreferences. In this way you could update inset and delete all information about the game in the proper way. If you need to delete data after you close the game just erase sharedPreferences on the onDestroy() life circle;

@override
public void onDestroy()
{
    super.onDestroy();
    SharedPreferences myPrefs = this.getSharedPreferences("examlePrefs");
    myPrefs.edit().remove("example");
    myPrefs.edit().clear(); 
    myPrefs.edit().commit();    
}

Or please use.

@override
public void onStop()
{
    super.onStop();
    SharedPreferences myPrefs = this.getSharedPreferences("examlePrefs");
    myPrefs.edit().remove("example");
    myPrefs.edit().clear(); 
    myPrefs.edit().commit();    
}

As i can understand you do not know lifecycle of the activity.So this article will explain how to use them.

http://developer.android.com/training/basics/activity-lifecycle/index.html

Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
  • except onDestroy does not always get called right away by the OS so the data can still be there when the app is opened again if the app is still in memory – tyczj Aug 13 '14 at 20:00
  • onPause() it is solution too. – Oleg Gordiichuk Aug 13 '14 at 20:01
  • How do this work? Where do I insert the delete methods in the code? Is there some way that I can program so that everytime the application is closed all the data from sharePreferences is gone? onDestory(), isn't that the method for closing an acitivty? (Sorry for my newbie questions ;) ) – user3925710 Aug 16 '14 at 10:30