49

I have an activity that i only want to run when the application is ran for the first time.

And never again. It is a facebook login activity. I only want to launch it once when the app is initially opened for the first time.

How do i go about doing this?

coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118

7 Answers7

66

What I've generally done is add a check for a specific shared preference in the Main Activity : if that shared preference is missing then launch the single-run Activity, otherwise continue with the main activity . When you launch the single run Activity create the shared preference so it gets skipped next time.

EDIT : In my onResume for the default Activity I do this:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean previouslyStarted = prefs.getBoolean(getString(R.string.pref_previously_started), false);
    if(!previouslyStarted) {
        SharedPreferences.Editor edit = prefs.edit();
        edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE);
        edit.commit();
        showHelp();
    }

Basically I load the default shared preferences and look for the previously_started boolean preference. If it hasn't been set I set it and then launch the help file. I use this to automatically show the help the first time the app is installed.

shivtej
  • 633
  • 9
  • 18
Femi
  • 64,273
  • 8
  • 118
  • 148
22

Post the following code within your onCreate statement

   Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
            .getBoolean("isFirstRun", true);

    if (isFirstRun) {
        //show start activity

        startActivity(new Intent(MainActivity.this, FirstLaunch.class));
        Toast.makeText(MainActivity.this, "First Run", Toast.LENGTH_LONG)
                .show();
    }


       getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                .putBoolean("isFirstRun", false).commit();

Replace FirstLaunch.class with the class that you would like to launch

troyhector
  • 321
  • 2
  • 3
14

something like this might work.

public class MyPreferences {

    private static final String MY_PREFERENCES = "my_preferences";  

    public static boolean isFirst(Context context){
        final SharedPreferences reader = context.getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE); 
        final boolean first = reader.getBoolean("is_first", true);
        if(first){
            final SharedPreferences.Editor editor = reader.edit();
            editor.putBoolean("is_first", false);
            editor.commit();
        }
        return first;
    }

}

usage

boolean isFirstTime = MyPreferences.isFirst(CurrentActivity.this);
if (isFirstTime) {
    NewActivity.show(CurrentActivity.this);
}

...

public class NewActivity extends Activity {
    public static void show(Context context) {
        final Intent intent = new Intent(context, NewActivity.class);
        context.startActivity(intent);
    }
}
Samuel
  • 9,883
  • 5
  • 45
  • 57
  • So in the onCreate() i could test for the IS_FIRST boolean, if its there then continue. If not then stop and alert the user or whatever i want to do? – coder_For_Life22 Aug 30 '11 at 04:52
  • I am not sure if it is handled different Java but I have used your code in C# xamarin.android and it should be other way around. reader.getBoolean will return "false" if no value is saved at all. so first variable is false at the first time running. which makes logic should be other way around. – Emil Apr 18 '22 at 00:17
4
SharedPreferences dataSave = getSharedPreferences("firstLog", 0);

if(dataSave.getString("firstTime", "").toString().equals("no")){ // first run is happened
}
else{ //  this is the first run of application
SharedPreferences.Editor editor = dataSave.edit();
                editor.putString("firstTime", "no");
                editor.commit();
}
1

I had done this without Shared Prefrence...as I know shared prefrence consumes some memory so I used public static boolean variable in global class....First I made Global Class Appconfig...and then I made boolean static variable like this :

public class Appconfig {

    public static boolean activity = false;
}

then I used this public static boolean variable into my welcome Activity class. I am Using License agreement page. which I have to use only at once in my application then never display further whenever i run the application. so i had put condtion in welcome activity...if the welcome class run first time so the static boolean variable is false...

 if (Appconfig.activity == false) {
Intent intent = new Intent();
intent.setClass(WelcomeActivity.this,LicesnceActivity.class);
startActivity(intent);
WelcomeActivity.this.finish();
}
if (Appconfig.activity == true) {

Intent intent = new Intent();
intent.setClass(WelcomeActivity.this, MainActivity.class);
    startActivity(intent);

}

Now at Licesnce Activity class I made :

Appconfig.activity=true;

So whenever I run the Application the second activity "Main activity" run after Welcome activity not License Activity....

Jinal Jogiyani
  • 1,199
  • 1
  • 9
  • 5
0

Declared in the globally

   public int count=0 
   int tempInt = 0;

in your onCreate function past this code at first.

   count = readSharedPreferenceInt("cntSP","cntKey");
   if(count==0){
       Intent intent = new Intent();
       intent.setClass(MainActivity.this, TemporaryActivity.class);
       startActivity(intent);
       count++;
       writeSharedPreference(count,"cntSP","cntKey");
       }

Past these two method outside of onCreate

    //Read from Shared Preferance
    public int readSharedPreferenceInt(String spName,String key){
    SharedPreferences sharedPreferences = getSharedPreferences(spName,Context.MODE_PRIVATE);
    return tempInt = sharedPreferences.getInt(key, 0);
    }     

    //write shared preferences in integer
    public void writeSharedPreference(int ammount,String spName,String key ){

    SharedPreferences sharedPreferences = getSharedPreferences(spName, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putInt(key, ammount);
    editor.commit();
}
Atiar Talukdar
  • 668
  • 11
  • 22
0

This is my code to bring the OnBoarding Activity for the first time. If it's not the first time then go directly to the Home Activity.

    private void checkFirstOpen(){
    Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
            .getBoolean("isFirstRun", true);

    if (!isFirstRun) {
        Intent intent = new Intent(OnBoardingScreen.this, Home.class);
        startActivity(intent);
        finish();

    }

    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putBoolean("isFirstRun", 
    false).apply();
}