如何在应用程序首次运行时仅显示一次视图



我想开发一个应用程序,在安装后显示一次身份验证屏幕,然后在后续运行时显示其他屏幕。有没有办法做到这一点?

使用 SharedPreference 存储firstboot值,并根据该值签入活动。如果设置了该值,则应用程序之前已启动。否则,将显示活动并在SharedPreference中设置firstrun标志。

例如,您启动的活动可能如下所示,

public void onCreate(){
    boolean firstboot = getSharedPreference("BOOT_PREF", MODE_PRIVATE).getBoolean("firstboot", true);
    if (firstboot){
        // 1) Launch the authentication activity
        // 2) Then save the state
        getSharedPreference("BOOT_PREF", MODE_PRIVATE)
            .edit()
            .putBoolean("firstboot", false)
            .commit();
    }
}

使用SharedPreference你可以做到这一点布尔旗清真寺 ;

public static void saveflagmosque(){
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean("mosque", false);
        editor.commit();
    }
    public boolean getflagmosque(){
        flagmosque = sharedPref.getBoolean("mosque", true);
        return flagmosque;
    }

在代码中

flagmosque = true ;
if(getflagmosque()){
your task that run only one time ;
}

您可以使用"共享首选项"来执行此操作。像 isFirstTime 这样的boolean变量可以看到您的工作。

最新更新