复制 activity 以使用唯一标识符启动相同 activity 的多个实例

Duplicate activity to launch many instances of same activity using unique identifier

我正在构建一个聊天客户端,与每个用户聊天都会打开相同的 activity ChatActivity 传递用户名作为 intent extras,就像这个一样。

Intent.putExtra("user", username);

其中用户名是您正在与之聊天的用户名。发送 extra 后,ChatAcitivity 打开,处理 extra 的用户名并相应地工作。现在,如果我同时与多个用户聊天,比如 user1 和 user2,每次 activity 重新启动以处理数据。我怎样才能保留与用户的聊天并打开已经初始化的 activity 以减少服务器和用户的负载。

我想要一些东西来识别需要打开的意图,比如与 user1 聊天将 user1 ChatActivity 带到前面,所以我可以使用:

resultIntent.setFlags(Intent.FLAG_ACTIVITY_USER1ACTIVITY_BROUGHT_TO_FRONT_);

或其他名称。

我该怎么做?

我知道您想要 'duplicate' 个 ChatActivity 实例,每个用户一个。您可以使用 FLAG_ACTIVITY_NEW_DOCUMENT 标志,文档是与特定用户的对话:

    // the URI is used as a document ID
    // a stack / activity with the same id will be reused
    // and the intent passed to onNewIntent()
    // otherwise a new activity is created
    startIntent.setData(Uri.parse("http://my.domain.net/users/" + username));
    startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);

请注意,每个对话在“最近”屏幕(“概览”屏幕)中都有自己的条目。这可能是也可能不是您想要的。