【问题标题】:On-boarding slider screen入职滑块屏幕
【发布时间】:2019-05-01 19:40:34
【问题描述】:

我使用了一个内置库来创建“android 载入滑块屏幕”。该库是实现 'com.github.apl-devs:appintro:v4.2.3'。介绍屏幕应该只在应用程序启动时第一次打开,但每次我运行我的应用程序时都会打开。怎么发射才第一次?

public class IntroActivity extends AppIntro {
private PrefManager prefManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addSlide(AppIntroFragment.newInstance("First","This is the first page",
            R.drawable.sugar, ContextCompat.getColor(getApplicationContext(),R.color.colorAccent)));
    addSlide(AppIntroFragment.newInstance("Second","This is the second page",
            R.drawable.baseline_card_giftcard_black_24dp, ContextCompat.getColor(getApplicationContext(),R.color.colorPrimary)));
    addSlide(AppIntroFragment.newInstance("Third","This is the third page",
            R.drawable.baseline_fastfood_black_18dp, ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark)));
}

@Override
public void onDonePressed(Fragment currentFragment) {
    super.onDonePressed(currentFragment);
    Intent intent = new Intent(IntroActivity.this,MainActivity.class);
    startActivity(intent);
}

@Override
public void onSkipPressed(Fragment currentFragment) {
    super.onSkipPressed(currentFragment);

    Intent intent = new Intent(IntroActivity.this,MainActivity.class);
    startActivity(intent);
}
}

【问题讨论】:

  • 您需要在首选项中保存一个变量,比如 shouldShowOnboarding,并在每次启动时检查该值。一旦用户跳过或完全看到了入职屏幕,请将其更新为 false。
  • 闪屏可以根据 shouldShowonboarding 偏好值决定是否打开介绍活动或任何其他屏幕

标签: android slider screen splash-screen


【解决方案1】:

来自库的文档,可用here

最后,像这样在 Manifest 中声明活动:

<activity android:name="com.example.example.intro"
android:label="@string/app_intro" />

不要将简介声明为您的主应用启动器,除非您希望在每次应用启动时都启动简介。有关如何从主要活动启动一次介绍的示例,请参阅 wiki。

这就是Wiki 所指的:

如果上述方法不清楚或您无法实现相同的方法,请尝试在 MainActivity.java 文件中编写以下使用 SharedPreferences 的代码:-

/* In your onCreate method */
SharedPreferences sp = getSharedPreferences(MyPrefs, Context.MODE_PRIVATE);
    if (!sp.getBoolean("first", false)) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("first", true);
        editor.apply();
        Intent intent = new Intent(this, IntroActivity.class); // Call the AppIntro java class
        startActivity(intent);
    }

此代码读取一个共享首选项,如果发现它不存在,或者它的值为 false,它会创建或编辑首选项(以便下次条件失败)然后打开介绍屏幕。

【讨论】:

  • 经过 50 次左右的编辑,这应该是您整理出来所需的一切。
  • 很高兴为您提供帮助,如果您的问题已解决,请不要忘记将问题标记为已回答。
猜你喜欢
  • 2021-12-15
  • 2018-06-28
  • 2022-01-06
  • 2023-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-17
相关资源
最近更新 更多