简体   繁体   中英

How to make a piece code function only once during launch?

My Primary Goal: LoginActivity is the default activity to be launched. I want my homeactivity to be launched at first WITHOUT using android.manifest due to some issues I can't change or mess with it. In my homeactivity I want a button or a floating action button to lead to the LoginActivity back.

Explanation of how I attempted to do this: I am using SendBird SDK's sample program to build my app over it. But I don't want the LoginActivity of the SendBird SDK to load at first I instead want my HomeActivity to load first so I used the following code in my LoginActivity.java onCreate.

LA#

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        Intent startActivity = new Intent(LoginActivity.this, HomeActivity.class);
        startActivity(startActivity);
        finish();
    }
}, 0001);

In my home activity I made a floating action button to lead back to the LoginActivity using intent

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

Then I executed the application and as expected the HomeActivity appeared first. Then I clicked the floating action button and again HomeActivity appeared. I know the problem that this is happening because of that code (LA#) in LoginActivity.java because when the Floating Action Button leads to Login Activity that code (LA#) tells the LoginActivity to again go back to HomeActivity so it is a continuous cycle. I now want to know how can I make that piece of code (LA#) in LoginActivity work only once during launch so that from next time onwards it doesn't go back to HomeActivity and just stays at LoginActivity.

Or is there any alternate way to achieve my goal?

Simple solution would be to have a boolean in a sqlite database, when the app first starts up, load the initial activity, set that boolean to true if the activity succeeds, then store that in the database, every time the app loads up after that, check that value in the database and if it's true, skip over that activity; Hacky but straightforward enough to implement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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