4

I have a UI-less MainActivity, which holds certain logic to determine if the user is already logged in or not. If the user is logged in, I am starting HomeActivity and if not, then LoginActivity. Even though the logic is working fine, I am seeing that the MainActivity does start for a brief while before switching to desired activity. Is there any way to avoid bringing up MainActivity altogether as it causes an annoying flicker ?

Here is my code (omitting the logic) -

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (condition) {
            intent = new Intent(this, HomeActivity.class);
        } else {
            intent = new Intent(this, LoginActivity.class);
        }

        startActivity(intent);
        finish();
    }
}

Thanks in advance.

Edit

This is what I have in my android manifest -

    <activity
        android:name="org.step.main.MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleInstance" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Sam
  • 4,302
  • 12
  • 40
  • 74
  • Are you trying in your device or in the emulator? – Joaquin Iurchuk Jul 07 '15 at 15:09
  • I am trying on the device. – Sam Jul 07 '15 at 15:10
  • Definitely the problem is not here. Not in that code. Except that the `condition` is very very heavy to compute. – Joaquin Iurchuk Jul 07 '15 at 15:12
  • Anyways, can you avoid the no-UI solution? Try to put the HomeActivity code in Main Activity and do the logic there, as you're doing it now, and if the user is not logged in then you finish actual and you start the LoginActivity. – Joaquin Iurchuk Jul 07 '15 at 15:14
  • The condition fetches a key-value pair from sharedpreferences and assigns them to a local variable, if present. – Sam Jul 07 '15 at 15:15
  • @joaquin Ya, I tried it earlier, by launching LoginActivity first and then checking the condition, and if true then switching to HomeActivity. But, same result occurred. As LoginActivity had a UI, so I thought of adding the logic to a UI-less activity like above. But the problem is still there. – Sam Jul 07 '15 at 15:18
  • Then the problem is in your device. Try your app in an emulator or in another device, please – Joaquin Iurchuk Jul 07 '15 at 15:18
  • was your device rooted? I am experiencing the same behavior, but only on rooted devices and emulators. – Dakatine Feb 06 '19 at 21:11

1 Answers1

19

You can try setting FLAG_ACTIVITY_NO_ANIMATION on the intent

intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

and/or call overridePendingTransition(0, 0) after calling finish()

You could also replace the activities with Fragments - have the MainActivity with a fullscreen scontainer that you replace with a login Fragment or a Home Fragment.

Malthan
  • 7,005
  • 3
  • 20
  • 22
  • 1
    So, I added the flag and overridePendingTransition as you mentioned and it worked fine. Thanks for the help. – Sam Jul 07 '15 at 15:50