如何使介绍滑块仅在安装后出现一次?

时间:2018-06-10 14:58:28

标签: java android

我制作了一个带this library的简介滑块。我希望滑块在应用安装后只出现一次。怎么做?

这是我的代码

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);
}
}

1 个答案:

答案 0 :(得分:2)

我使用SharedPreferences类来完成此任务。将此代码放在onCreate()方法的欢迎活动中。

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));
    }

在SliderActivity类中创建一个全局变量。

public static final String COMPLETED_ONBOARDING_PREF_NAME = "Onboarding Completed";

将此代码放在finishTutorial()方法中。

@Override
public void finishTutorial(){
SharedPreferences.Editor sharedPreferencesEditor =
            PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit();
    sharedPreferencesEditor.putBoolean(
            COMPLETED_ONBOARDING_PREF_NAME, true);
    sharedPreferencesEditor.apply();

    finish();
}

此代码存储天气与否用户已在应用偏好设置中完成教程,除非他们卸载应用或清除应用数据,否则用户无法更改。如果您有任何其他问题,请与我们联系。