1

How can I keep track of the first launch of my app and redirect user to another screen? Please, give me example codes.

  • 4
    You can use Shared Preferences. This will help you: http://stackoverflow.com/questions/7217578/check-if-application-is-on-its-first-run – Augusto Carmo Aug 26 '16 at 23:07

3 Answers3

3

Do you mean that from a launcher activity to anthor activity?If so, you can try this:

In your Launcher Activity(MainAcitivity):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SharedPreferences preferences = this.getPreferences(MODE_PRIVATE);
    if (preferences.getBoolean("firstLaunch",false)) {
        Intent intent = new Intent(MainActivity.this,AnotherActivity.class);
        startActivity(intent);
    }else{
        preferences.edit().putBoolean("firstLaunch",true).commit();
    }
}
CoXier
  • 162
  • 1
  • 10
1

Use SharedPrefernces for storing the FirstLogin or not.

SharedPreferences prefs =getSharedPreferences("packagename", MODE_PRIVATE);

    if (prefs.getBoolean("firstrun", true)) {
        // Do first run stuff here then set 'firstrun' as false
        prefs.edit().putBoolean("firstrun", false).commit();
    }
    else{
       // Do if not first launch
    }
Asif Patel
  • 1,744
  • 1
  • 20
  • 27
1

That code is working for me:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    SharedPreferences prefs =getSharedPreferences("packagename", MODE_PRIVATE);
    if (prefs.getBoolean("firstrun", true)) {
        Intent intent = new Intent(MainActivity.this,AnotherActivity.class);
        startActivity(intent);
        prefs.edit().putBoolean("firstrun", false).commit();
    }
    else{}
}