Save
Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Essential
These cookies are needed to make the website work correctly. You can not disable them.
Affichage
Accept
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline

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 related 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 parameter initialized
*/
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:
year 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 gold 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
This email address is being protected from spambots. You need JavaScript enabled to view it.
Author Android2EE
EBooks to learn Android Programming.
AndroidEvolution

Find me on Google +.
Follow me on Twitter
Join my LinkedIn network or Viadeo

Original author: mathias
Save
Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Essential
These cookies are needed to make the website work correctly. You can not disable them.
Affichage
Accept
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline