I have this activity, called DataActivity, and I want it to show up the first time I open my app, and never again after. Than I want my MainActivity to be my first Activity, can someone tell me how i do this? (I'm pretty new to coding so a bit of code would be appreciated...) Thanks! Kind regards! :)
Asked
Active
Viewed 537 times
1
-
After you call startActivity(intent) to launch your MainActivity, call finish(); on the DataActivity. – mjp66 Nov 21 '15 at 19:54
-
2Possible duplicate of [How to launch activity only once when app is opened for first time?](http://stackoverflow.com/questions/7238532/how-to-launch-activity-only-once-when-app-is-opened-for-first-time) – Hussein El Feky Nov 21 '15 at 21:26
2 Answers
1
Try below code :
public class HelperActivity extends Activity {
SharedPreferences prefs = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Perhaps set content view here
prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
}
@Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
// start DataActivity because its your app first run
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
startActivity(new Intent(HelperActivity.this , DataActivity.class));
finish();
}
else {
startActivity(new Intent(HelperActivity.this , MainActivity.class));
finish();
}
}
}
This activity will help you decide if its first run of an application and start respective activity according to that by storing boolean value in sharedPreference
Make this activity your launcher activity.

Christian
- 21
- 1
- 3
- 7

Bhargav Thanki
- 4,924
- 2
- 37
- 43
0
You could save an entry to SharedPreferences
indicating that the DataActivity
has been shown once, and then in DataActivity
onCreate()
you could do something like:
boolean dataShownOnce = checkSharedPreferencesForDataActivityShown();
if (dataShownOnce) {
// Go to MainActivity
startActivity(new Intent(this, MainActivity.class);
finish();
} else {
// Showing DataActivity for the first time, continue with the normal logic
}
There's a pretty good tutorial on using SharedPreferences
here: http://developer.android.com/training/basics/data-storage/shared-preferences.html

ehehhh
- 1,066
- 3
- 16
- 27
-
thanks! had an answer that was kinda the same thing, but thanks for that tutorial! helped me a lot further as well! – h-beneuh Nov 22 '15 at 00:49