Waneya Iqbal Waneya Iqbal - 4 months ago 16x
Android Question

Check if application is on its first run

I am new to android development and and I want to setup some of application's attributes based on Application first run after installation. Is there any way to find that the application is running for the first time and then to setup its first run attributes?


The following is an example of using SharedPreferences to achieve a 'first run' check.

public class MyActivity extends Activity {

    SharedPreferences prefs = null;

    protected void onCreate(Bundle savedInstanceState) {
        // Perhaps set content view here

        prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);

    protected void onResume() {

        if (prefs.getBoolean("firstrun", true)) {
            // Do first run stuff here then set 'firstrun' as false
            // using the following line to edit/commit prefs
            prefs.edit().putBoolean("firstrun", false).commit();