Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity that is only launched once after a new install?

I want my app to have an activity that shows instruction on how to use the app. However, this "instruction" screen shall only be showed once after an install, how do you do this?

like image 656
borislemke Avatar asked Jan 08 '12 18:01

borislemke


People also ask

How do you launch an activity only once for the first time?

Now in the onResume() method, check if we already have the value of prevStarted in the SharedPreferences. Since it will evaluate to false when the app is launched for the first time, start the MainActivity (Which we want to appear just once) and set the value of prevStarted in the SharedPreferences.

Can activity have only one task?

The activity can only be running as the root activity of the task, the first activity that created the task, and therefore there will only be one instance of this activity in a task.

Which launch mode creates an activity in new task?

Single Top If an instance does not exist on top of the task, a new instance will be generated. You can use this launch mode to generate numerous instances of the same activity within the same task or across tasks, but only if the identical instance does not already exist at the top of the stack.


1 Answers

You can test wether a special flag (let's call it firstRun) is set in your application SharedPreferences. If not, it's the first run, so show your activity/popup/whatever with the instructions and then set the firstRun in the preference.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences settings = getSharedPreferences("prefs", 0);
    boolean firstRun = settings.getBoolean("firstRun", true);
    if ( firstRun )
    {
        // here run your first-time instructions, for example :
        startActivityForResult(
             new Intent(context, InstructionsActivity.class),
             INSTRUCTIONS_CODE);

    }
 }



// when your InstructionsActivity ends, do not forget to set the firstRun boolean
 protected void onActivityResult(int requestCode, int resultCode,
         Intent data) {
     if (requestCode == INSTRUCTIONS_CODE) {
         SharedPreferences settings = getSharedPreferences("prefs", 0);
         SharedPreferences.Editor editor = settings.edit();
         editor.putBoolean("firstRun", false);
         editor.commit();
     }
 }
like image 60
Emmanuel Sys Avatar answered Oct 23 '22 00:10

Emmanuel Sys