SoulRayder
SoulRayder

Reputation: 5166

How to track if an app is launching for the first time in Android?

I am developing an app in which for only the first time it is launched, I want to perform some actions. Now I considered using Shared Preferences till I hit the dilemma that I'd have to initialize that on Oncreate itself, and every time I launch the app, the shared preferences would be overwritten.

So, I am considering checking whether a particular type variable itself exists on the Shared Preferences or not, but I am stuck there too. Now is there a simpler way that I am overlooking?

Upvotes: 1

Views: 1735

Answers (3)

Ksharp
Ksharp

Reputation: 102

SharePreferences is a good choice.

public class ShortCutDemoActivity extends Activity {

// Create Preference to check if application is going to be called first
// time.
SharedPreferences appPref;
boolean isFirstTime = true;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Get preference value to know that is it first time application is
    // being called.
    appPref = getSharedPreferences("isFirstTime", 0);
    isFirstTime = appPref.getBoolean("isFirstTime", true);

    if (isFirstTime) {
        // Create explicit intent which will be used to call Our application
        // when some one clicked on short cut
        Intent shortcutIntent = new Intent(getApplicationContext(),
                ShortCutDemoActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);
        Intent intent = new Intent();

        // Create Implicit intent and assign Shortcut Application Name, Icon
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Demo");
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(
                        getApplicationContext(), R.drawable.logo));
        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(intent);

        // Set preference to inform that we have created shortcut on
        // Homescreen
        SharedPreferences.Editor editor = appPref.edit();
        editor.putBoolean("isFirstTime", false);
        editor.commit();

    }
}

}

And modify your Androidmanifest.xml

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

Upvotes: 0

Deepak Jangir
Deepak Jangir

Reputation: 590

Use this first time SharePrefrences Code:

SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        if (!prefs.getBoolean("Time", false)) {

                           // run your one time code

            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("Time", true);
            editor.commit();
        }

This sharedpreference is run only one time when first time is application is launched. This is work for me.

Upvotes: 5

kalyan pvs
kalyan pvs

Reputation: 14590

For this you need to check like this..

/**
 * checks for the whether the Application is opened first time or not
 * 
 * @return true if the the Application is opened first time
 */
public boolean isFirstTime() {
    File file = getDatabasePath("your file");
    if (file.exists()) {
        return false;
    }
    return true;
}

if the file is exist its not the first time other wise its the first time..

Upvotes: 1

Related Questions