Reputation: 593
I have made an intro slider with this library . I want the slider to appear only once after the app installation. How to do this?
This is my code
public class SliderActivity extends TutorialActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addFragment(new Step.Builder().setTitle("This is header")
.setContent("This is content")
.setBackgroundColor(Color.parseColor("#3F51B5")) // int background color
.setDrawable(R.drawable.image_1) // int top drawable
.setSummary("This is summary")
.build());
addFragment(new Step.Builder().setTitle("This is header")
.setContent("This is content")
.setBackgroundColor(Color.parseColor("#FF4081")) // int background color
.setDrawable(R.drawable.image_3) // int top drawable
.setSummary("This is summary")
.build());
addFragment(new Step.Builder().setTitle("This is header")
.setContent("This is content")
.setBackgroundColor(Color.parseColor("#f816a463")) // int background color
.setDrawable(R.drawable.image_4) // int top drawable
.setSummary("This is summary")
.build());
}
@Override
public void finishTutorial() {
Intent intent = new Intent(SliderActivity.this, WelcomeActivity.class);
startActivity(intent);
}
}
Upvotes: 0
Views: 626
Reputation: 1020
I used the SharedPreferences class to accomplish this. Put this code in your welcome activity in your onCreate() method.
private SharedPreferences sharedPreferences;
sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
// Check if we need to display our OnboardingFragment
if (!sharedPreferences.getBoolean(
SliderActivity.COMPLETED_ONBOARDING_PREF_NAME, false)) {
startActivity(new Intent(this, SliderActivity.class));
}
Create a global variable in your SliderActivity class.
public static final String COMPLETED_ONBOARDING_PREF_NAME = "Onboarding Completed";
And place this code in your finishTutorial() method.
@Override
public void finishTutorial(){
SharedPreferences.Editor sharedPreferencesEditor =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit();
sharedPreferencesEditor.putBoolean(
COMPLETED_ONBOARDING_PREF_NAME, true);
sharedPreferencesEditor.apply();
finish();
}
This code stores weather or not the user has completed the tutorial in app preferences and can't be changed by the user unless they uninstall the app or clear app data. Let me know if you have any other questions.
Upvotes: 2