Welcome Android Developpers

Welcome Android Developpers
My name is Mathias Séguy, I am an Android Expert and Trainer and created the Android2EE company.

For full information, it’s here : Android2EE's Training
You can meet me on Google+, follow me on Twitter or on LinkedIn

Tuesday, November 15, 2011

Initialize your application when first launched on the device.




Bandeau Conference
Hello,

Today I submit a simple way to implement actions at the first launch of your application on a phone. In other words, how to customize the first launch of your application?

The idea is quite simple and relies on the use of the object SharedPreferences. This object is used to store a set of key-value specific to your application within the system and be able to CRUD (create, read, update, delete) them easily.
The idea is to use this HashMap to retrieve ​​specific values of your application. In particular I would call a Boolean Initialized that lets you know if your application has already been launched on the Android device. The following code shows how to do (this code is in a class that extends Activity in the onCreate method):



//Attributes
  /**
   * The key of the SharedPreference
   */
  public static final String KEY = "MyProgramName";
  /**
   * The key for the initialized parameter
   */
  public static final String INITIALIZED = "initialized";
....

//In the onCreate method:


  // Check if the application has been initialized
    // Retrieve the object sharedPreference of the application
    SharedPreferences savedSession = activity.getSharedPreferences(MyLightConstants.KEY, Context.MODE_PRIVATE);
    // Retrieve the initiliazed attribute (if the key is not found, this call returns FALSE)
    initialized = savedSession.getBoolean(INITIALIZED, Boolean.FALSE);
    //When launching for the first time the application, initialized==FALSE
    if (!initialized) {
      //Set the initialized to TRUE
      Editor editor = activity.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
      editor.putBoolean(INITIALIZED, Boolean.TRUE);
      editor.commit();
     //And do your initilisation actions:

       an action
    } 




Note that this trick is a special case of the persistence of data within the application. Indeed, to persist your data you can use either the methods onSaveInstance and onRestoreInstance (I described their use in the Android2EE's Ebooks) either the principle of sharedPreferences. It is recommended to use in most cases the onSaveInstance and onRestoreInstance methods that work in exactly the same way. The use of shardePreference brings flexibility in the timing of the restoration and / or saving (especially for those that not happen in the onResume or OnPause methods even if to explain the principle that's where I positioned the recordings / restorations). Below, i explain the generic use of the sharedPreference object.
In onPause you back up your data. In the method onResume you restore them.


To save your data (in the onPause method so):

      //Retrieve the object SharedPreference in the edit mode
      Editor editor = activity.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
      //Adding or changing the value of a key (putInt, putString,PutBoolean...):
      editor.putBoolean(MY_KEY, MyValue);
      //And especially do not forget to commit the changes to persist
      editor.commit();


To restore your data (in the onResume method so):

    //Retrieve the object SharedPreference in the read mode
    SharedPreferences savedSession = getSharedPreferences(MyLightConstants.KEY, Context.MODE_PRIVATE);
    //Retrieve the value (associated to the key MY_INT_KEY)
    //If the key is not found the call will return the default value defined by the second parameter (here
    // 255))
    savedSession.getInt(MY_INT_KEY, 255);


So, Thanks who?
Thanks, Android2ee, the Android Programming Ebooks :o)

Mathias Séguy
mathias.seguy.it@gmail.com
Auteur Android2EE
Ebooks to learn Android Programming.
AndroidEvolution

Retrouvez moi sur Google+
Suivez moi sur Twitter
Rejoignez mon réseau LinkedIn ou Viadeo

No comments:

Post a Comment