user2515973
user2515973

Reputation: 17

How to make Activity to start only once when the app is started for the first time?

I have a Registration Activity, which I want to be started only once when the app is started for the first time. If the registration is made, the second time when the app is started I want to go directly to the second Activity - FirstWindow.

Thank you in advance!

Upvotes: 1

Views: 5462

Answers (3)

Neron T
Neron T

Reputation: 369

Use a zero activity to check what to launch next, onCreate:

SharedPreferences sp= getSharedPreferences("first_time", 0);

ActivityZero.this.finish();

if (sp.getBoolean("FirstTime", true))
   mainIntent = new Intent(ActivityZero.this, TutorialActivity.class);              
else
   mIntent = new Intent(ActivityZero.this, MainActivity.class);

ActivityZero.this.startActivity(mIntent);

Upvotes: 0

KOTIOS
KOTIOS

Reputation: 11196

when the activity is started for the first time : save true value in shared pref and everytime the app launches check the shared pref if true go to next activity else show first activity (ur registration page)

1.Declare variables

SharedPreferences pref;
SharedPreferences.Editor editor;

2.in onCrete method

pref = getSharedPreferences("testapp", MODE_PRIVATE);
editor = pref.edit();

3.When user successfully registers (click on register button)

editor.putString("register","true");
editor.commit();

Then every time u can check by :

String getStatus=pref.getString("register", "nil");
if(getStatus.equals("true"))
redirect to next activity
else
show registration page again

Upvotes: 6

Mus
Mus

Reputation: 1860

Create an activity with Theme.NoDisplay and make it your launcher activity (set intent filter for launcher in the manifest). In the onCreate, check if the user has registered or not and launch the appropriate activity. You can store the status of the user (registered/unregistered) in a DB or in SharedPreferences.

Upvotes: 0

Related Questions