【问题标题】:How to add flags to an APPWIDGET_CONFIGURE intent?如何将标志添加到 APPWIDGET_CONFIGURE 意图?
【发布时间】:2013-04-10 05:26:14
【问题描述】:

我在APPWIDGET_CONFIGURE注册了一个活动:

    <activity android:name="com.tahanot.activities.NearbyStops">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
        </intent-filter>
    </activity>

但是,此活动的行为与预期不同。它在现有堆栈内打开,当我按下后退按钮时,它会将我带到其他活动而不是关闭任务。理想情况下,我希望 APPWIDGET_CONFIGURE 意图包括 FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_MULTIPLE_TASK

是否可以在AndroidManifest.xml 中指定标志,如果不能,您会建议什么解决方法?

【问题讨论】:

    标签: android android-intent android-widget android-activity intentfilter


    【解决方案1】:

    考虑为活动元素指定launchMode属性。

    <activity android:launchMode="singleTask" android:name="com.tahanot.activities.NearbyStops">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
        </intent-filter>
    </activity>
    

    根据official docs

    FLAG_ACTIVITY_NEW_TASK

    在新任务中启动活动。如果任务已经在运行 您现在开始的活动,该任务被带到 恢复其最后状态的前景并且活动接收到 onNewIntent() 中的新意图。

    这会产生与 "singleTask" launchMode 值相同的行为, 在上一节中讨论过。

    既然你提到你想要 FLAG_ACTIVITY_NEW_TASK 行为,所以 singleTask launchMode 可能适合你。

    【讨论】:

    • @apppsroxcom,非常感谢您的指点!这是有史以来最简单的赏金决定 :) 实际上我最终使用了“singleTop”,因为“singleTask”没有做我需要的事情。我还为这个活动添加了一个独特的 android:taskAffinity,以确保它是在单独的任务上打开的。所以最后,按下后​​退按钮不会返回到任何其他活动。
    • 感谢您的赏金。标志的组合如何产生不同的结果真的很有趣。我很高兴你找到了适合你的那个。干杯!
    • 奇怪的是,将“singleTask”添加到 APPWIDGET_CONFIGURE 导致了一个大问题:启动器在配置后没有将小部件放置在屏幕上。很难找到这个标志是原因。
    【解决方案2】:

    当你开始你的活动时使用这个 java 代码

    Intent intent = new Intent(this,
            activityname.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    

    在这里,您可以像这样使用您的意图对象添加标志。

    您还可以添加多个标志。

    【讨论】:

    • 它不会开始另一个活动,所以我将有两个活动而不是一个?
    • 我尝试将此代码放在 onCreate 中,它确实创建了另一个活动。它会导致活动无限循环。
    【解决方案3】:

    这是我使用的清单声明,遵循appsroxcom 对android:launchMode 的想法:

    <activity
        android:name="com.tahanot.activities.NearbyStops"
        android:configChanges="orientation"
        android:label="@string/title_activity_stops_nearby"
        android:launchMode="singleTop"
        android:taskAffinity="com.tahanot.tasks.widgetConfiguration" > 
        <!-- android:launchMode makes sure the Back button doesn't navigate to another NearbyStops activity. 
             android:taskAffinity makes sure the Back button doesn't navigate to some other activity. -->
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
        </intent-filter>
    </activity>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多