【问题标题】:Kill all instances of an Activity杀死一个活动的所有实例
【发布时间】:2014-05-01 10:49:05
【问题描述】:

我正在我的应用程序中设置通知,我在测试时注意到,在单击新通知后(在这种情况下,通知会加载博客详细信息页面),我有许多博客详细信息活动实例正在运行(按返回它显示每个活动以及以前加载的博客)。

在我的 Receiver 类中是否有可能,所以看看是否有 ActivityBlog 的任何实例已经在运行,如果所有 .finish() 都在运行,那么只有一次实例在运行?

我找到了this,但我想不出办法。

【问题讨论】:

  • 为什么不使用singleInstance作为这个活动的启动模式

标签: android android-activity android-notifications


【解决方案1】:

你应该学习活动启动模式 http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

在清单文件的元素中使用 android:launchMode="singleTop"。 如果活动实例已经启动,您将在 onNewIntent() 中获得回调。您的活动堆栈将自动更新,并且不需要杀死消耗时间和资源的活动。 我认为这是推荐的方法。

【讨论】:

  • 像魅力一样工作!
【解决方案2】:
    Intent z = new Intent(Projects_Accel.this,MainActivity.class);
    z.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
    Intent.FLAG_ACTIVITY_CLEAR_TASK |
    Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(z);

使用它来杀死所有活动

【讨论】:

    【解决方案3】:

    这样做

    Intent intent = new Intent(this, ActivityBlog.class);
    ComponentName cn = intent.getComponent();
    Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
    activity.startActivity(mainIntent);
    

    注意:

    你需要把android-support.jar放到libs文件夹中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多