Take the 2-minute tour ×

In my project I am to provide some functions which can be provided using session and application variable like in c#. MY information is confidential so can not be stored in files like xml Thanks in advance.

Edit : 1- Solution for session variable i've found called sharedpreference but don't know how to use please tell me

  • how to initiallize it and
  • set it from other class and
  • get the value from some other?


share|improve this question
SharedPreferences is (stored in xml format and) also readable and can be found and explored, see here and here since you said info is confidential. –  Adil Soomro May 21 '12 at 5:27
add comment

2 Answers

up vote 1 down vote accepted

Application class is there for you. use it and save your application level data, like this:

public class WhatEverApp extends Application
    String mApplicationLevelVar = "Hello";

WhatEverApp will be the name of your app used in manifest.xml

Look here for detailed discussion on Application class.

share|improve this answer
What happens when the app gets killed and your data disappears? –  Kristopher Micinski May 20 '12 at 17:20
What about session variable ? –  Prime Minister of India May 20 '12 at 17:21
@KristopherMicinski: yea..!! data stored in vars is cleared. –  Adil Soomro May 21 '12 at 5:29
so whats the solution –  Prime Minister of India May 21 '12 at 6:14
suppose if i am to create a hit count like variable that how many times my application has been used then how i could be able to use it –  Prime Minister of India May 21 '12 at 6:16
add comment

First of all, your application can basically die at any time, because it can be killed off by the Android scheduler after your app sits in the background for a while and the platform needs more memory.

That being said, storing your data in a global Application class is generally considered a bit hacky. Instead, the way you use this depends on how you are going to access the application. It's a pretty commonly accepted thing that a lot of apps keep things like their OAuth tokens in SharedPreferences (though I'm not sure exactly how kosher this is). What you might do is keep your session variable in an Application class, and then -- whenever the app dies -- simply reauthenticate. This is probably good practice anyway, as after that long someone may have picked up the phone, etc...

However, you seem to be under the impression that SharedPreferences can be read by anyone. This is incorrect, see this. Now, if you have a rooted phone, sure, then there's a way around that, but this is always going to be an issue, on a rooted phone you should basically consider that you don't really have any security at all...

share|improve this answer
add comment

Your Answer


