Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

android - Check if an application is on its first run with Kotlin

Basically, I want to have a screen/view that will open when the user opens up the app for the first time. This will be a login screen type of thing.

there are classes SplashActivity

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)
    }
}

class 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

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()
    }

}

We suggest to don't declare MyCustomAppIntro as your first Activity unless you want the intro to launch every time your app starts. Ideally you should show the AppIntro activity only once to the user, and you should hide it once completed (you can use a flag in the SharedPreferences) ????

question from:https://stackoverflow.com/questions/65651306/check-if-an-application-is-on-its-first-run-with-kotlin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use SharedPreference for that Issue, its quite easy and simple approach:

Splash 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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...