简体   繁体   English

当我使用FLAG_ACTIVITY_SINGLE_TOP-onDestroy()使我的活动保持活跃

[英]When I use FLAG_ACTIVITY_SINGLE_TOP - onDestroy() keep my activity alive

When my service need to interact with the user it starts an activity: 当我的服务需要与用户互动时,它会启动活动:

Intent i = new Intent(context, Ringer_intent.class);
i.putExtra("PHONE_NUMBER", phone_number);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
// TODO: This prevents onDestroy --> why???
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

context.startActivity(i);

In order to make sure that Ringer_intent will not run twice I added 为了确保Ringer_intent不会运行两次,我添加了

i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

And I also added to my activity 而且我还添加了我的活动

@Override
public void onNewIntent (Intent intent) {
}

I want my Ringer_intent activity to go away when it finishes. 我希望我的Ringer_intent活动结束后消失。 I do not want the user to be able to bring it back to the fg. 我不希望用户能够将其带回fg。
That the way it work w/o FLAG_ACTIVITY_SINGLE_TOP 没有FLAG_ACTIVITY_SINGLE_TOP的工作方式
But with FLAG_ACTIVITY_SINGLE_TOP my activity remains in the bg ... 但是使用FLAG_ACTIVITY_SINGLE_TOP,我的活动仍保留在背景中...
I added the following: 我添加了以下内容:

@Override
public void onStop() { Log.i(LOG_TAG, "Ringer_intent.onStop()"); // For Debugging
    super.onStop();
}

@Override
public void onDestroy() { Log.i(LOG_TAG, "Ringer_intent.onDestroy()"); // For Debugging
    super.onDestroy();
    Log.e(LOG_TAG, "Still not dead - kill it!");
    // Bad idea - it kills my service as well !!!
    // android.os.Process.killProcess(android.os.Process.myPid());
}

So I can see that Androids is calling both 因此,我可以看到Android同时调用了
But my activity is still there ... 但是我的活动仍然在那里...
Any idea? 任何想法?
Thanks 谢谢

Note: 注意:
I tried to call finish() from onDestroy() - did not help 我试图从onDestroy()调用finish()-没有帮助

You cannot call Intent.setFlags() the way you are doing it. 您不能按Intent.setFlags()的方式调用Intent.setFlags() When you call setFlags() it overwrites the flags with the argument you pass to setFlags() . 调用setFlags()它将使用传递给setFlags()的参数覆盖标志。 This means that after calling setFlags() three times, the only flag that will be set will be the one you specified in the last call to setFlags() . 这意味着,在三次调用setFlags()之后,将要设置的唯一标志将是您在上一次调用setFlags()指定的setFlags() You can use addFlags() instead of setFlags() , or you can call setFlags() once and pass all of the flags as the argument, like this: setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_SINGLE_TOP) 您可以使用addFlags()而不是setFlags() ,也可以一次调用setFlags()并将所有标志作为参数传递,如下所示: setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_SINGLE_TOP)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM