0

I have used shared preference for session maintaining in my application. And storing userId and password in Shared preference . How to clear these data when I exit from my application. Because when I restart my application It will give the previous session. When I log out in my application and again start app it will work nicely. In logout I have made the shared preference data (userId and password) as null. but I want to clear all these data in application exit.

I have also used below permission in manifest file: "android.permission.CLEAR_APP_USER_DATA"

<uses-permission android:name="android.permission.CLEAR_APP_USER_DATA"></uses-permission>

But I dont know how to use them in coding.

Community
  • 1
  • 1
Subrat
  • 3,928
  • 6
  • 39
  • 48
  • When I am clearing data as this: Menu - Settings - Applications - Manage Applications -chose - Clear data. It will work nicely and clear the data – Subrat May 22 '11 at 13:10

4 Answers4

5

I have used shared preference for session maintaining in my application.

Do not do that. Use static data members for "session" data and use shared preferences for persistent data.

How to clear these data when I exit from my application .

There is no concept of exiting an application in Android any more than there is a concept of exiting an application on the Web.

If you use static data members for your session data, the data will automatically be cleared when Android terminates the process, which it will do as a matter of course when the application has been abandoned for a significant period of time.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

You can use the code below for developing and debugging, but not for production. Android applications are not supposed to be 'exited', see here

public class MyActivity extends Activity {

     protected void onStop()
     {
        SharedPreferences settings = getSharedPreferences(PREF_FILE_NAME, 0);
        Editor e = settings.edit();
        e.clear(); 
        e.commit();
     }
 }
Community
  • 1
  • 1
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
0

Do not use shared preferences. Just use global variables instead. Something like this should do it:

class Globals {
    static public String userid;
    static public String pasword;
}
Philip Sheard
  • 5,789
  • 5
  • 27
  • 42
0

Try to fix your problem using tutorial from here: http://www.hrupin.com/2011/11/how-to-clear-user-data-in-your-android-application-programmatically There is a sample project in this post.

Hope it help you!

ihrupin
  • 6,932
  • 2
  • 31
  • 47