Lilian Sorlanski
Lilian Sorlanski

Reputation: 435

How to finish an activity before is being displayed?

I have in my app two activities. This is how I added them in the Manifest file:

<activity android:name=".auth.SignInActivity"/>
<activity android:name=".main.MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

In both activities I have a listener that helps me check the state of the user. If the user is logged-in I send the user to the MainActivity otherwise to the SignInActivity.

The problem is when the user is not logged-in. When opening the app, the user is redirected to the MainActivity. Since to check if the user is logged-in takes some time, the users sees the MainActivity for a few seconds till is redirected to the SignInActivity.

So when the user is not logged in, how can I stop the MainActivity from being displayed even for those few seconds?

Upvotes: 2

Views: 160

Answers (3)

AFLAH ALI
AFLAH ALI

Reputation: 451

You can use the Using a Launcher Theme with a Dedicated Splash Activity method as mentioned as method 2 at https://android.jlelse.eu/the-complete-android-splash-screen-guide-c7db82bce565

this is the right way to do it

Upvotes: 1

Angeo Johny M
Angeo Johny M

Reputation: 59

Either you can include a splashscreen to get your job done. Or else you can call your setcontentview() or corresponding ui elements after your checking method gets done.

Upvotes: 3

Bach Vu
Bach Vu

Reputation: 2348

In this case, you can create a SplashActivity where you can check user's state

class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        //show a splash image and check user's state then redirect to correct screen
    }
}

Upvotes: 2

Related Questions