调用finish()后,活动布局闪烁 [英] Activity layout blinking after finish() is called

查看:283
本文介绍了调用finish()后,活动布局闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打开我的应用程序时,将启动一个Activity,并在其onCreate方法中检查某些条件.如果条件成立,请完成当前活动并打开另一个活动.问题是:第一个活动在屏幕上闪烁,然后打开第二个活动.代码如下:

When I open my app, an Activity is started, and inside its onCreate method I'm checking some conditions. If the condition is true, I finish my current Activity and open another one. The problem is: The first activity blinks on the screen and then the second one is opened. The code is below:

public class FirstActivity extends Activity {
    @Override
    protected final void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //some code here...
        checkSomeStuff();
        setContentView(R.layout.my_layout);
        //some code here...
    }
    private void checkSomeStuff() {
        if (/*some condition*/) {
            final Intent intent = new Intent(this, SecondActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            finish();
            startActivity(intent);
        }
    }
}

请注意,setContentView()在检查之后,但是在第二个活动开始之前,第一个活动仍在屏幕上闪烁. 有谁知道如何使其不眨眼?

Notice that setContentView() is after the check, but before the second activity is started, the first one still blinks on the screen. Does anyone know how to make it not blink?

谢谢.

推荐答案

finish()的目的是销毁当前活动并将其从后堆栈中删除.通过调用finish然后触发意图,您是在要求活动将其自身销毁(我想眨眼是它试图恢复),然后将意图触发到第二个意图.将完成移到startActivity()之后

The purpose of finish() is to destroy the current activity and remove it from the back stack. By calling finish then firing the intent, you are asking the activity to destroy it self (I assume the blink is it trying to recover) then firing the intent to the second. Move finish to after startActivity()

 @Override
protected final void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //some code here...
    if(checkSomeStuff()) {
         setContentView(R.layout.my_layout);
         //some code here...
    }
}

private boolean checkSomeStuff() {
    if (/*some condition*/) {
        final Intent intent = new Intent(this, SecondActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
        return false;
    }
    return true;
}

这篇关于调用finish()后,活动布局闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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