从小部件启动应用程序:没有FLAG_ACTIVITY_BROUGHT_TO_FRONT的影响

时间:2013-05-28 10:32:41

标签: android android-activity widget flags

我只是想将我的应用程序小部件用作应用程序的快捷方式,我不想打开新任务。 所以我使用了FLAG_ACTIVITY_BROUGHT_TO_FRONT标志:

        Intent configIntent = new Intent(context, MainActivity.class);
        configIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        PendingIntent configPendingIntent = PendingIntent.getActivity(
                context, 0, configIntent, 0);
        views.setOnClickPendingIntent(R.id.widget, configPendingIntent);

但不幸的是,每次点击小部件时,都会启动一项新活动。

我错过了什么吗?

编辑:我也尝试过:FLAG_ACTIVITY_REORDER_TO_FRONT

2 个答案:

答案 0 :(得分:2)

除了我评论的内容外:

configIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    <activity
        android:name="com.source.myApp"
        android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  

如果它已将其启动模式声明为“多个”(默认值)并且您没有在同一意图中设置FLAG_ACTIVITY_SINGLE_TOP,那么它将被完成并重新创建;对于所有其他启动模式或者如果设置了FLAG_ACTIVITY_SINGLE_TOP,则此Intent将被传递到当前实例的onNewIntent()。

API reference

答案 1 :(得分:0)

我试过的每个标签都惨遭失败......

我不得不使用android:launchMode="singleInstance"使小部件以与用户离开的状态相同的状态启动我的Activity。