2

I have a help activity in my app and I want it to only launch in first run.

I have tried this:

In on create of Help Activity:

SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", true);
editor.commit();

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

In onResume of Help Activity:

@Override
public void onResume() {
super.onResume();
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", true);
if (!firstRun) {
    Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    Log.d("TAG1", "firstRun(false): " + Boolean.valueOf(firstRun).toString());
} else {
    Log.d("TAG1", "firstRun(true): " + Boolean.valueOf(firstRun).toString());
}
}

In onCreate of MainActivity:

SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", false);
editor.commit();

boolean firstRun = settings.getBoolean("firstRun", true);
Log.d("TAG1", "firstRun: " + Boolean.valueOf(firstRun).toString());

But it doesn't shows help activity, and when it runs the app it just jumps into MainActivity!!

And I have an exit button in my app and when I want to exit the app with that button it just shows again MainActivity and didn't exit the app.

Psypher
  • 10,717
  • 12
  • 59
  • 83
  • where have you specified to jump to the helpActivity? – Lal Jun 20 '15 at 17:43
  • First tip: The code in `onCreate()` of HelpActivity is not needed. Since you specify true as the default, you don't need to set it to true before `onResume()`, since it will default to true if there is no value present. As for the exit button issue, please add your on click code for that button. Also, please add your AndroidManifest.xml. – Daniel Nugent Jun 20 '15 at 17:50
  • 1
    @DanielNugent thank you for answer but I'm basic in android programming can you help me with codes? for example edit my wrong code to correct one and I'll accept your answer ;-) –  Jun 20 '15 at 17:57
  • Which activity is defined as the main/launcher activity? – M4rtini Jun 20 '15 at 18:04
  • In onCrate of Help Activity you are always starting a net MainActivity activity without checking firstRun. – M4rtini Jun 20 '15 at 18:07
  • @M4rtini Help Activity is the main. if you know answer please please help with codes I'll accept your answer –  Jun 20 '15 at 18:20
  • I would have set the `MainActivity`as the launcher activity. And then launch the HelperActicity from MainActivity that one time it is needed. If not you would be creating two activities on every app launch, which would slow down the startup. – M4rtini Jun 20 '15 at 18:26

1 Answers1

3

Do this in onCreate of MainActivity. That is all you should need if you set MainActivity as the launcher activity. Which is what i would recommend.

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

boolean firstRun = settings.getBoolean("firstRun", true);
if (firstRun) {
    settings.edit().putBoolean("firstRun", false).apply();
    //start help activity
}

With MainActivity as the launcher activity you should get faster launches, as you don't create two activities every time. And you avoid having two activities in the back stack.

ps: Google does not recommend the usage of an "exit" button. Rather you should rely on the back\home button to close the app and let the OS decide when the app is destroyed.
Is quitting an application frowned upon?

Community
  • 1
  • 1
M4rtini
  • 13,186
  • 4
  • 35
  • 42
  • 1
    so if i set MainActivity as luncher activity then I can't open help activity as a first run activity :/ –  Jun 20 '15 at 18:51
  • The code above will launch the helpActivity on the first launch. Did that not work? – M4rtini Jun 20 '15 at 18:53
  • Can you describe what happens? – M4rtini Jun 20 '15 at 19:01
  • Try to uninstall and install the app again. The value of "firstRun" might be sticking around from before. If that don't help, then please add an edit to the question of how your code now looks. – M4rtini Jun 20 '15 at 19:08