Reputation: 1276
I have a help_view that have to show only once when user first open the app when installed. When user uninstall the application and re install it the view must show.
I tried to implement this by shared preferences. See below by code ;
private void gotoMainActivity() {
SharedPreferences helpinfo = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = helpinfo.edit();
boolean help = helpinfo.getBoolean("help", false);
if(help==false){
Intent intent = new Intent(this.getApplicationContext(),HelpActivity.class);
startActivity(intent);
}else{
Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
I realize I have to update shared preference when first login. Please help me to do this.
Upvotes: 0
Views: 65
Reputation: 4547
// SharedPreferences
mSharedpreferences = getApplicationContext().getSharedPreferences("MyPref1", Context.MODE_PRIVATE);
if (!mSharedpreferences.contains("help")) {
// Shared preference not present create it. First time launch and set it with default value
mSharedpreferences.edit().putBoolean("help", true).commit();
Intent intent = new Intent(this.getApplicationContext(), HelpActivity.class);
startActivity(intent);
} else {
boolean help = helpinfo.getBoolean("help", false);
if (!help) {
Intent intent = new Intent(this.getApplicationContext(), HelpActivity.class);
mSharedpreferences.edit().putBoolean("help", true).commit();
startActivity(intent);
} else {
Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
Upvotes: 0
Reputation: 1230
You have to update code like this:
private void gotoMainActivity() {
SharedPreferences helpinfo = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = helpinfo.edit();
boolean help = helpinfo.getBoolean("help", false);
if(!help){
Intent intent = new Intent(this.getApplicationContext(),HelpActivity.class);
editor.putBoolean("help",true);
startActivity(intent);
}else{
Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
Upvotes: 1
Reputation: 8149
private void gotoMainActivity() {
SharedPreferences helpinfo = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if(!helpinfo.getBoolean("help", false)){
// First Launch
helpinfo.edit().putBoolean("help",true).apply();
}else{
// Not a first launch
}
}
Upvotes: 0
Reputation: 1493
try this
private void gotoMainActivity() {
SharedPreferences helpinfo = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = helpinfo.edit();
boolean help = helpinfo.getBoolean("help", false);
if(!help){
Intent intent = new Intent(this.getApplicationContext(),HelpActivity.class);
editor.putBoolean("help",true).commit();
startActivity(intent);
}else{
Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
Upvotes: 0
Reputation: 2430
You should put your boolean in the following code
if(help==false){
Intent intent = new Intent(this.getApplicationContext(),HelpActivity.class);
editor.putBoolean("help",true);
editor.apply();
startActivity(intent);
}else{
Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
startActivity(intent);
}
Upvotes: 0