onCreate中的moveTaskToBack导致活动短暂显示然后隐藏 [英] moveTaskToBack in onCreate causes activity to show briefly and then hide

查看:270
本文介绍了onCreate中的moveTaskToBack导致活动短暂显示然后隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个BroadcastReceiver,它接收推送通知, 然后,我开始一个活动并显示通知. (请注意,当用户操作通知时,该活动并未开始)

I have a BroadcastReceiver that receives a push notification, I then start an activity and show a notification. (notice that the activity is not started when the user actions the notification)

我的意图是在后台模式"下启动活动,然后在用户响应通知时将活动置于最前面.

My intention is to start the activity in "background mode" and then when the user responds to the notification bring the activity to the front.

除了活动简要显示然后一切正常外,其他所有工作均正常进行 立即隐藏. (闪烁一秒钟).

All is working perfectly except that the activity briefly shows and then immediately hides. (flashing activity for a second).

这是我的代码:

BroadcastReceiver...
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("BACKGROUND",true);
        context.startActivity(intent);


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

        if (getIntent().getBooleanExtra("BACKGROUND",false)) {
            moveTaskToBack(true);
        } else {

        }

推荐答案

出现相同的问题.通过在主题中添加android:windowDisablePreview属性来解决.

Had the same problem. Solved by adding android:windowDisablePreview attribute in your theme.

例如,在您的res/values/styles.xml中:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowDisablePreview">true</item>
    </style>

</resources>

不确定是否可以通过编程方式添加它.

Not sure if there is any way to add it programatically though.

这篇关于onCreate中的moveTaskToBack导致活动短暂显示然后隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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