在条件下启动活动会在屏幕上产生闪烁 [英] Starting Activity on condition produces a flicker on screen

查看:68
本文介绍了在条件下启动活动会在屏幕上产生闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个没有UI的MainActivity,它具有确定用户是否已经登录的某些逻辑.如果用户已登录,则我将启动HomeActivity,否则,将启动LoginActivity.尽管逻辑工作正常,但我发现MainActivity确实会短暂启动,然后再切换到所需的活动.有什么方法可以避免完全引起MainActivity的问题,因为它会引起烦人的闪烁?

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

谢谢.

修改

这就是我的android清单中的内容-

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>

推荐答案

您可以尝试在意图上设置FLAG_ACTIVITY_NO_ANIMATION

You can try setting FLAG_ACTIVITY_NO_ANIMATION on the intent

intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

和/或在调用finish()之后调用overridePendingTransition(0, 0)

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

您还可以用Fragments替换活动-使MainActivity具有全屏标记,并用登录Fragment或Home Fragment替换.

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.

这篇关于在条件下启动活动会在屏幕上产生闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆