How can I keep track of the first launch of my app and redirect user to another screen? Please, give me example codes.
Asked
Active
Viewed 150 times
1
-
4You 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 Answers
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
-
-
@SummerAugust Have you tried it? I tried it just now and it did work for me.When you first launch app ,you will come into MainActivity.Later you will come into AnotherActivity – CoXier Aug 27 '16 at 06:14
-
-
Sorry I made a mistake just now.When you first launch app ,you will come into AnotherActivity.And then ,when you launch app ,you will come into MainActivity.Am I right? – CoXier Aug 27 '16 at 06:23
-
If you only use a `SharedPreference`,you can use `Activity.getSharedPreference(int mode)` – CoXier Aug 27 '16 at 06:29
-
-
-
-
-
i already use this in my code: SharedPreferences prefs =getSharedPreferences("packagename", MODE_PRIVATE); – SummerAugust Aug 27 '16 at 06:41
-
I mean if you use only a `SharedPreferences` object,you can use `getPreferences(MODE_PRIVATE)` – CoXier Aug 27 '16 at 06:43
-
-
-
-
-
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{}
}

SummerAugust
- 27
- 7