-2

I want to execute a specific command only in the first application lunch and when the user opens the application again another command will be executed instead . Here's an example

if (this_is_first_lunch == true){
Toast.makeText(getApplicationContext(), "First time to open the app", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Not the first time to open it", Toast.LENGTH_SHORT).show();
}

My question here is what should I write instead of "this_is_first_lunch" to make this code working?

matiash
  • 54,791
  • 16
  • 125
  • 154
AlphaCode
  • 439
  • 1
  • 3
  • 17
  • 2
    Have you thought about using `SharedPreferences` – EdmDroid May 05 '14 at 15:37
  • 2
    You can use shared preference for that.. – Lal May 05 '14 at 15:38
  • Well I have read about it and I'm currently trying to apply it but the problem is how can I assign a value for this boolean this_is_first_lunch only once . I mean how can I give it a true value but only within the 1st lunch. – AlphaCode May 05 '14 at 15:39

4 Answers4

4

Use SharedPreferences for that. You should store a key after the "first time" code is executed and check it on subsequent executions.

matiash
  • 54,791
  • 16
  • 125
  • 154
2

Use shared preference like this like this

private static String KEY_FIRST_RUN = "";
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    sharedPreferences = getPreferences(MODE_PRIVATE);

    if (!sharedPreferences.contains("KEY_FIRST_RUN")) {
        KEY_FIRST_RUN = "something";
        Log.d("First", "First run!");
    } else {
        Log.d("Second...", "Second run...!");
    }

    editor = sharedPreferences.edit();
    editor.putString("KEY_FIRST_RUN", KEY_FIRST_RUN);
    editor.commit();
}
Lal
  • 14,726
  • 4
  • 45
  • 70
2

You can use the Shared Preferences to attach a persistent value which you can read each time the program starts. When you start the app, check if this variable is set. Use that to determine which code to execute and then set the variable to keep it from happening again the next time you run.

boolean firstRun = getSharedPreferences("preferences", MODE_PRIVATE).getBoolean("firstrun", true);
if(firstRun){
    //set the firstrun to false so the next run can see it.
    getSharedPreferences("preferences", MODE_PRIVATE).edit().putBoolean("firstrun", false).commit();
    Toast.makeText(getApplicationContext(), "First time to open the app", Toast.LENGTH_SHORT).show();
}
else{
    Toast.makeText(getApplicationContext(), "Not the first time to open it", Toast.LENGTH_SHORT).show();
}
Matt O
  • 1,336
  • 2
  • 10
  • 19
0

You need to persist, that the app was launched before... Here is the Android Dev Guide on how to generally persist data: http://developer.android.com/guide/topics/data/data-storage.html

After persisting the first l a unch, you can check for that flag or whatever you persisted to recognize... Keep in mind, that this requires the app to read that flag on every start, which will probably not slow it down, but can stack up to more such Activitys and influence startup performance of your app ...

EdmDroid
  • 1,350
  • 1
  • 11
  • 25
smnpl
  • 589
  • 5
  • 6