Reputation: 15
I want to ask for username at very first time when application launched after installation and after that user screen will not come ever and on MainActivity screen should greet the user.
Upvotes: 0
Views: 1333
Reputation: 91
You should use the SharedPreferences, so you can save the username at the first time and when the user opens the application at the second time, you can check if the username exists in the SharedPreferences : if yes you need to show the main activity, if no you need to show the login activity.
Add this public static variable in any class,, in order to access it from the login activity and from the startup of the application, so this is the key of your Shared Preferences
public static final String SHARED_PREF = "MyPrefsFile";
In the method that should save the user name in the login activity you should add
SharedPreferences settings = getSharedPreferences(<class>SHARED_PREF, 0); // 0 - for private mode
SharedPreferences.Editor editor = settings.edit();
editor.putString("UserName", "User X");
editor.putBoolean("hasLoggedIn", true);
editor.commit();
In order to prevent the user from using the back button to go back to the Login activity you should finish it before starting the main.activity
Intent intent = new Intent();
intent.setClass(Forwarding.this, ForwardTarget.class);
startActivity(intent);
this.finish();
And at the startup of the application
SharedPreferences settings = getSharedPreferences(<class>.SHARED_PREF, 0);
boolean hasLoggedIn = settings.getBoolean("hasLoggedIn", false);
if(hasLoggedIn)
{
//Go to main activity.
}
else
{
//Go to login activity
}
Upvotes: 1