-1

My application in android can remember its static member value of a class after closing application and running again. I don't save anything to sd card or anywhere else. How can it be? I use to close the application by following code

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    return super.onKeyDown(keyCode, event);
}

My class and its static values

public class General {
    static float colorx = 7, colory = 607;
    ...
    }
adba
  • 477
  • 9
  • 25
  • where is the static member in your code. no the value is not persisted. if you want the value to persist use shared preferences – Raghunandan Oct 29 '13 at 04:44
  • 2
    What do you mean by "close your application", if you use Back or Home button to just "get out from your application" it doesn't mean you close it, the application just simply brought back to the background and it still keeping its variables. That why when it runs again, those values still remains. – user2652394 Oct 29 '13 at 04:44
  • I supposed that my application is closed when I pressed the back button. Because it isn't seen in the active application list. If it is not closed when I pressed back button how can I close it completely? – adba Oct 29 '13 at 04:50
  • @Raghunandan I have edited my question to show the static values. – adba Oct 29 '13 at 05:02
  • 1
    @adba http://stackoverflow.com/questions/13126833/does-the-garbage-collector-work-on-static-variables-or-methods-in-java. check this. so when the applications process is killed the values is reset and not persisted. if you want the value to persist use shared preferences – Raghunandan Oct 29 '13 at 05:05
  • As far as I understand, I should use instance fields if I don't want to see the values again after closing application. Thanks for the link. – adba Oct 29 '13 at 05:15
  • 1
    @adba don't use static variables and why is the need for one.http://developer.android.com/training/articles/perf-tips.html. if you need to pass values between activities use intents – Raghunandan Oct 29 '13 at 05:23

2 Answers2

1

Try to do the garbage collection manually, that should solve the problem.

Mrinal Saurabh
  • 948
  • 11
  • 16
0

For this purpose the only solution is to use SharedPreferences. You can store variable data in SharedPreferences and then retrieve it though your application closed before.

androidcodehunter
  • 21,567
  • 19
  • 47
  • 70