【问题标题】:Android: how to prevent multiple instances of an activity to be launched from a widget?Android:如何防止从小部件启动多个活动实例?
【发布时间】:2011-12-07 14:29:36
【问题描述】:

重现问题的步骤:

  • 用户启动我的应用(根活动的名称:“mainActivity”)=> mainActivity 的实例 A
  • 他按下主页按钮(mainActivity 在后台运行)
  • 他安装了与此应用相关的小部件
  • 他单击小部件 => 显示 mainActivity 的新实例(实例 B)
  • 他点击后退按钮:用户回到活动 A(我不想要什么!活动 B 应该关闭(实际上,整个应用程序应该关闭))

你知道如何避免这个问题吗? (我在 stackoverflow 上看到过一些类似的问题,但严格来说不是我想要的)

谢谢!!!!

代码:

public class MyWidgetProvider extends AppWidgetProvider {

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    // Build the intent to call the service//

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

    Intent openAppIntent = new Intent(context.getApplicationContext(), MainActivity.class);
    openAppIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
    PendingIntent openAppPendingIntent = PendingIntent.getActivity(context, 0, openAppIntent, 0);


    remoteViews.setOnClickPendingIntent(R.id.widgetLinearLayout, openAppPendingIntent);

//// ETC…///

    }

【问题讨论】:

    标签: android android-activity android-intent widget


    【解决方案1】:

    尝试使用:

    openAppIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    

    description here.


    你也可以使用:

    openAppIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
    

    description here.

    【讨论】:

    • 感谢 NeTeInStEiN 和 Craigy,但我仍然有同样的问题......另一个想法?
    • 您需要在打开包括清单在内的活动时设置标志(包括从应用程序抽屉启动)。这是正确答案,因此其他想法对您没有帮助。
    • 嗨,库夫斯。我尝试将 openAppIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) 然后 openAppIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) 添加到我的 onUpdate 方法,但它不起作用。我的问题的不同步骤中描述的问题仍然存在。我哪里错了?顺便说一句,“包括在清单中”是什么意思?谢谢。
    • 他的意思是(我认为)在清单中定义一个启动模式。 developer.android.com/guide/topics/manifest/…
    【解决方案2】:

    我建议使用:

    openAppIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    

    这将重用已经存在的ActivityonNewIntent 将被调用。您可以根据需要从那里更新 UI。

    更多参考请查看:Developer.android.com - FLAG ACTIVITY SINGLE TOP

    编辑
    Launcher Activity 是在 AndroidManifest.xml 中具有以下意图过滤器的一个

            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
    

    如果小部件启动一个拥有自己的活动堆栈的新任务,这些标志将不起作用。

    我认为最好是阅读这篇文章Developer.android.com - ACTIVITY TASK DESIGN

    如果您有任何发现,请告诉我们。

    【讨论】:

    • 谢谢 Gyuri,但我仍然有同样的问题......另一个想法?
    • 你的MainActivity是启动器活动吗?
    • MainActivity 是应用从头启动时启动的activity;所以,是的,我想这就是你所说的“启动器活动”。问题是,当应用程序从头开始启动时,会启动 MainActivity 的第一个实例,然后当我单击它时,小部件会启动第二个实例。然后我叠加了2个实例;当我单击后退按钮时,我从“小部件 mainActivity”返回到“启动器 mainActivity”。任何想法 ?非常感谢!!!!
    • 在我的答案框中查看我的回复。
    【解决方案3】:

    我已通过将此属性添加到清单中成功地阻止了一个活动的多个实例:

    android:launchMode="singleInstance"
    

    这实际上告诉 Android 这个活动是 Highlander(“只能是一个”),并且会阻止它创建多个实例。无论何时打开相关活动,Android 都会将现有实例置于最前面,或者在某些情况下销毁并重新创建当前实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多