如何像启动画面一样只运行一次活动 [英] How to run an activity only once like Splash screen

查看:20
本文介绍了如何像启动画面一样只运行一次活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想在第一次运行时只运行一次启动画面,但问题是我已经在 Manifest 中放置了这一行:android:noHistory="true" 如果我按下后退按钮并退出应用程序,效果很好,但请注意,该应用程序仍在后台运行,当我按下应用程序图标时,它会再次返回初始屏幕,然后返回我的注册页面.我想在重新打开应用程序时直接重定向到注册页面.

In my app, I would like to run the Splash screen once at first run only but the problem is that I already placed in the Manifest this line: android:noHistory="true" which works great if I press back button and exits the app but note that the app is still in the background running, and when I press the app icon it goes back again to the Splash screen then my Registration page. I wanted to be redirected to the Registration page directly when I reopen my application.

我该怎么做?提前感谢您的任何建议.

How do I do this? Thanks ahead for any suggestions.

推荐答案

这就是我在 SplashActivity(onCreate) 中所做的:

So here's what I did, in my SplashActivity(onCreate):

    SharedPreferences settings = getSharedPreferences("prefs", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("firstRun", true);
    editor.commit();

    Intent intent = new Intent(this, RegistrationActivity.class);
    startActivity(intent);

SplashActivity(onResume):

SplashActivity(onResume):

@Override
public void onResume() {
    super.onResume();
    SharedPreferences settings = getSharedPreferences("prefs", 0);
    boolean firstRun = settings.getBoolean("firstRun", true);
    if (!firstRun) {
        Intent intent = new Intent(this, RegistrationActivity.class);
            startActivity(intent);
        Log.d("TAG1", "firstRun(false): " + Boolean.valueOf(firstRun).toString());
    } else {
        Log.d("TAG1", "firstRun(true): " + Boolean.valueOf(firstRun).toString());
    }
}

在我的 RegistrationActivity(onCreate) 中:

In my RegistrationActivity(onCreate):

    SharedPreferences settings = getSharedPreferences("prefs", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("firstRun", false);
    editor.commit();

    boolean firstRun = settings.getBoolean("firstRun", true);
    Log.d("TAG1", "firstRun: " + Boolean.valueOf(firstRun).toString());

然后禁用后退按钮以防止返回,除非用户按下主页:

And then disabled back button to prevent going back unless the user presses Home:

@Override
public void onBackPressed() {
}

非常感谢那些做出贡献的人!

Big thanks for those that contributed!

这篇关于如何像启动画面一样只运行一次活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆