繁体   English   中英

如何使启动画面加载一次?

[英]How do I make a splash screen load once?

所以...我已经开发了一个启动屏幕,可以成功运行。 如何使其运行一次(并且只能运行一次)? 我想建立一个注册屏幕,但我只希望它对用户出现一次。

救命!

阿玛尼·斯旺(Amani Swann)

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.content.Intent;
import com.nfc.linkingmanager.R;

public class SplashScreen extends Activity {

private boolean mIsBackButtonPressed;
private static final int SPLASH_DURATION = 1000; 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash_screen);

    Handler handler = new Handler();


    handler.postDelayed(new Runnable() {

        @Override
        public void run() {



            finish();

            if (!mIsBackButtonPressed) {

                Intent intent = new Intent(SplashScreen.this, NewCore.class);
                SplashScreen.this.startActivity(intent);
           }

        }

    }, SPLASH_DURATION);

}

 @Override
 public void onBackPressed() {
    mIsBackButtonPressed = true;
    super.onBackPressed();

}
}

使用SharedPreferences存储屏幕已经显示的事实; 开始时,请检查此项目,如果是,则通过启动下一个活动并立即为启动屏幕调用finish()来替换“活动”。

您需要将标记存储在这样的共享首选项中

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this );
finish();
if (prefs.getBoolean("SplashFlag", false)&&!mIsBackButtonPressed){

 Intent intent = new Intent(SplashScreen.this, NewCore.class);
                SplashScreen.this.startActivity(intent);
}else {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("SplashFlag", true); // value to store
//again there are values for int, long, string, float, boolean
editor.commit();//This is needed or the edits will not be put into the prefs file
}

使用SharedPreferences ,可以在其中存储一个boolean (例如seenSplash ,您可以将其默认设置为false。 然后使用if来检查它是否为假,然后显示启动画面。 用户看到启动画面后,将获得一个Editor ,您可以在其中将布尔值更改为true。

不要忘记在编辑器上commit()所做的更改。

在您的注册屏幕中,获取登录详细信息并与您保存在共享首选项中的值进行比较。 或硬编码,例如用户名是admin,密码是admin。 是两个值都为true只是为了进入启动屏幕,否则可以随意重定向到其他页面。

有两种方法可以做到这一点

1

finish();
SplashScreen.this.startActivity(intent);

在您的Android清单中的第2个您宣布启动屏幕活动的位置

加上这个

android:noHistory=true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM