Android 检查应用程序是否第一次使用Kotlin运行

Android 检查应用程序是否第一次使用Kotlin运行,android,android-studio,kotlin,Android,Android Studio,Kotlin,基本上,我希望有一个屏幕/视图,当用户第一次打开应用程序时打开。这将是一个登录屏幕类型的东西 这里有很多活动 class SplashActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) //hiding title bar of this activity

基本上,我希望有一个屏幕/视图,当用户第一次打开应用程序时打开。这将是一个登录屏幕类型的东西

这里有很多活动

class SplashActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //hiding title bar of this activity
        window.requestFeature(Window.FEATURE_NO_TITLE)
        //making this activity full screen
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        setContentView(R.layout.activity_splash)

        //4second splash time
        Handler().postDelayed({
            //start main activity
            startActivity(Intent(this@SplashActivity, MyCustomAppIntro::class.java))
            //finish this activity
            finish()
        },2000)
    }
}
类MyCustomAppIntro

class MyCustomAppIntro : AppIntro() {




    companion object {
        fun startActivity(context: Context) {
            val intent = Intent(context, MyCustomAppIntro::class.java)
            context.startActivity(intent)

        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setTransformer(AppIntroPageTransformerType.Depth)
        // You can customize your parallax parameters in the constructors.
        setTransformer(AppIntroPageTransformerType.Parallax(
                titleParallaxFactor = 1.0,
                imageParallaxFactor = -1.0,
                descriptionParallaxFactor = 2.0
        ))

             // Make sure you don't call setContentView!

        // Call addSlide passing your Fragments.
        // You can use AppIntroFragment to use a pre-built fragment
        addSlide(
            AppIntroFragment.newInstance(
            imageDrawable = R.drawable.ayana,
                backgroundDrawable = R.color.black,
                        description = "Привет мой друг"

        ))
        addSlide(
                AppIntroFragment.newInstance(
                        imageDrawable = R.drawable.ayana,
                        backgroundDrawable = R.color.black,
                        description = "Меня зовут AYANA"
        ))
        addSlide(
                AppIntroFragment.newInstance(
                        backgroundDrawable = R.drawable.screen_3

                ))
    }

    override fun onSkipPressed(currentFragment: Fragment?) {
        super.onSkipPressed(currentFragment)
        // Decide what to do when the user clicks on "Skip"
        val intent = Intent(this,MainActivity::class.java)
        startActivity(intent);


        finish()
    }

    override fun onDonePressed(currentFragment: Fragment?) {
        super.onDonePressed(currentFragment)
        val intent = Intent(this,MainActivity::class.java)
        startActivity(intent);
        finish()
    }
班级活动

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_activity_main)

        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)



        about.setOnClickListener{
            val intent = Intent(this,ScondActivity::class.java)
            startActivity(intent);
        }


        val assistantFragment = AimyboxAssistantFragment()

        supportFragmentManager.beginTransaction().apply {
            replace(R.id.assistant_container, assistantFragment)
            commit()
        }
    }

    override fun onBackPressed() {
        val assistantFragment = (supportFragmentManager.findFragmentById(R.id.assistant_container)
                as? AimyboxAssistantFragment)
        if (assistantFragment?.onBackPressed() != true) super.onBackPressed()
    }

}

我们建议不要将MyCustomAppIntro声明为您的第一个活动,除非您希望在每次应用启动时启动该简介。理想情况下,您应该只向用户显示一次AppIntro活动,并在完成后隐藏它(您可以在SharedReferences中使用标志)??

使用SharedReference解决该问题,其方法非常简单:

SharedPreference sharedpref = getApplicationContext.getSharedPreferences(SETTINGS_PREFERENCES,MODE_PRIVATE);

String token = sharedPreferences.getString("token", null);

if (token.equals("False") || token == null){

    //will call the view for the first and last time until cache is cleared

    //after trigger this login programmatically set the token value true thus you can solve the problem  
    
    SharedPreferences.Editor editor = sharedpref.edit();
                                    editor.putString("token", "True");
                                    editor.apply();

}else{
    //do something or redirect to login or main activity
}
飞溅活动:

SharedPreference sharedpref = getApplicationContext.getSharedPreferences(SETTINGS_PREFERENCES,MODE_PRIVATE);

String token = sharedPreferences.getString("token", null);

if (token.equals("False") || token == null){

    //will call the view for the first and last time until cache is cleared

    //after trigger this login programmatically set the token value true thus you can solve the problem  
    
    SharedPreferences.Editor editor = sharedpref.edit();
                                    editor.putString("token", "True");
                                    editor.apply();

}else{
    //do something or redirect to login or main activity
}

使用SharedReference解决这个问题,它非常简单:

SharedPreference sharedpref = getApplicationContext.getSharedPreferences(SETTINGS_PREFERENCES,MODE_PRIVATE);

String token = sharedPreferences.getString("token", null);

if (token.equals("False") || token == null){

    //will call the view for the first and last time until cache is cleared

    //after trigger this login programmatically set the token value true thus you can solve the problem  
    
    SharedPreferences.Editor editor = sharedpref.edit();
                                    editor.putString("token", "True");
                                    editor.apply();

}else{
    //do something or redirect to login or main activity
}
飞溅活动:

SharedPreference sharedpref = getApplicationContext.getSharedPreferences(SETTINGS_PREFERENCES,MODE_PRIVATE);

String token = sharedPreferences.getString("token", null);

if (token.equals("False") || token == null){

    //will call the view for the first and last time until cache is cleared

    //after trigger this login programmatically set the token value true thus you can solve the problem  
    
    SharedPreferences.Editor editor = sharedpref.edit();
                                    editor.putString("token", "True");
                                    editor.apply();

}else{
    //do something or redirect to login or main activity
}

您还可能需要检查
onCreate()
是否第一次运行:

if (savedInstanceState == null) {
    ...
}

您还可能需要检查
onCreate()
是否第一次运行:

if (savedInstanceState == null) {
    ...
}

正如@EmonHossainMunna所建议的,
SharedReferences
是可行的,kotlin代码如下

        val sharedpref: SharedPreferences =
            getApplicationContext().getSharedPreferences(
                "com.example.android.your_application",
                MODE_PRIVATE
            )

        val token: String? = sharedpref.getString("token", null)
        if (token == "False" || token == null) {
            // rest of the FirstTime Logic here

            sharedpref.edit().putString("token", "true").apply()
        } else {
            // rest of the Not-FirstTime Logic here
        }

正如@EmonHossainMunna所建议的,
SharedReferences
是可行的,kotlin代码如下

        val sharedpref: SharedPreferences =
            getApplicationContext().getSharedPreferences(
                "com.example.android.your_application",
                MODE_PRIVATE
            )

        val token: String? = sharedpref.getString("token", null)
        if (token == "False" || token == null) {
            // rest of the FirstTime Logic here

            sharedpref.edit().putString("token", "true").apply()
        } else {
            // rest of the Not-FirstTime Logic here
        }

非常感谢,但是需要kotlin进行语法编辑,其余代码都是一样的。它不起作用,SharedReferences Errors如果你有语法错误,只需使用kotlin语法,其余逻辑是相同的。对不起,我可以这样做。非常感谢,但是需要kotlin进行语法编辑,代码的其余部分是相同的。它不起作用,SharedReferences errorsHope如果你有语法错误,只需使用kotlin语法,其余的逻辑是相同的。对不起,我可以这样做,如果(savedInstanceState==null){startActivity(Intent(this@SplashActivity,MyCustomAppIntro::class.java))}else{startActivity(意图)(this@SplashActivity,MainActivity::class.java))}如果(savedInstanceState==null){startActivity(Intent),则}不起作用(this@SplashActivity,MyCustomAppIntro::class.java))}else{startActivity(Intent(this@SplashActivity,MainActivity::class.java))}无法运行超级,谢谢…超级,谢谢。。。