1

I am developing an Android app with a Navigation Drawer working this way: the user opens the app and a Tutorial (an activity with a pager sliding fragments, called ScreenSlideActivity.java) pops up; when the user finishes sliding the tutorial, he presses the "finish" button resulting in the initialization of the MainActivity (creating a drawerLayout, drawerToggle etc.)

What I need to do is opening the Tutorial activity just once, after the app's first launch.

I've tried with this code in the main Activity:

if (savedInstanceState == null) {
            SelectItem("tutorial");
}

making sure that ScreenSlideActivity.java is immediately started. The problem with this solution is that when the tutorial is opened, from there I'm not able to access MainActivity.java anymore, neither from the "up" botton neither from the Tutorial's last page's "finish" button, probably because for some reason I don't have the main as the parent activity anymore.

Then I've tried this solution change application's starting activity - Android modifying the manifest xml file. Adding:

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

to ScreenSlideActivity. The problem with this solution is that it changes the structure of my project, making my ScreenSlideActivity.java as the Main and Launching Activity (and therefore from here I cannot access the MainActivity anymore) while all I want to do is to display it once.

What else can I do?

Community
  • 1
  • 1
Cris
  • 2,002
  • 4
  • 30
  • 51

2 Answers2

1

What I usually to deal with temporary screens is similar to your first solution:

  1. In your MainActivity check if it is the first time the user launches the application. The most common way to do this is using SharedPreferences and checking if a boolean is true or false.
  2. If it is the first time, you start a new activity for your ScreenSlideActivity class with no particular flags. your MainActivity will be in the stack:

    MainsActivity -> ScreenSlideActivity
    
  3. When the users press the finish button you can call the finish() method on your ScreenSlideActivity which will remove the activity from the stack and bring back your MainActivity.

Quanturium
  • 5,698
  • 2
  • 30
  • 36
1

You can use SharedPreferences to check/set if it's the first time opening the app

String preferences_name = "isFirstTime";

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   ...

    firstTime();
}
...

public  void  firstTime(){

    SharedPreferences sharedTime = getSharedPreferences(preferences_name,0); 
    if (sharedTime.getBoolean("firstTime",true))
    {

        //Your tutorial code

        sharedTime.edit().putBoolean("firstTime",false).apply();
    }
    else
    {
        //When not using tutorial code

   }

}

Similiar question here : https://stackoverflow.com/a/13237848/4555911

Community
  • 1
  • 1
ii7scw
  • 351
  • 1
  • 3
  • 17
  • Great answer. I've also used the `finish()` on pression of the "finish" button, as suggested by Quanturium, in order to remove the second activity from the stack once the user navigates back to Main Activity. It worked smoothly. Thank you very much – Cris Mar 05 '15 at 10:21