Android - 如何创建一个活动的多个实例?

发布于 2024-09-08 07:29:04 字数 876 浏览 0 评论 0原文

我想知道是否可以在 Android 中创建单个 Activity 的多个实例?

我目前使用以下代码启动自己的通话屏幕进行 Voip 测试:


     public void initInCallScreen(String pName, String phoneNumber, int contactID, boolean 
        callDirection, int lineID){

    //starts in callScreen dialog
    final Intent myIntent = new Intent(context, CallDialogActivity.class);
    myIntent.putExtra("NAME", pName);
    myIntent.putExtra("NUMBER", phoneNumber);
    myIntent.putExtra("ID", contactID);
    myIntent.putExtra("CALLTYPE", callDirection); //True = Incoming, False = Outgoing
    myIntent.putExtra("LINEID", lineID);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);

这使我可以正常启动活动。

然而,当我调用它一秒钟时,它只是返回到已经创建的活动,而不是创建一个新的活动并将其放在堆栈上。

我希望能够多次创建该活动,以便堆栈上有两个或 3 个活动,并且用户可以使用“主页”、“后退”按钮等在它们之间切换...

这是否可能,如果可以,我在做什么错误的?

I was wondering is it possible to create multiple instances of a single Activity in Android?

I currently start my own inCall screen for a Voip Test by using the following code:


     public void initInCallScreen(String pName, String phoneNumber, int contactID, boolean 
        callDirection, int lineID){

    //starts in callScreen dialog
    final Intent myIntent = new Intent(context, CallDialogActivity.class);
    myIntent.putExtra("NAME", pName);
    myIntent.putExtra("NUMBER", phoneNumber);
    myIntent.putExtra("ID", contactID);
    myIntent.putExtra("CALLTYPE", callDirection); //True = Incoming, False = Outgoing
    myIntent.putExtra("LINEID", lineID);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);

This allows me to start the Activity fine.

However when I call it for a second it just returns to the Activity already created rather than creating a new Activity and placing it on the stack.

I would like to be able to create the activity multiple times so that I have two or 3 Activities on the stack and the user can switch between them, using Home, Back buttons etc...

Is this possible and if so what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巾帼英雄 2024-09-15 07:29:04

但是当我调用它时它
刚刚返回到 Activity
创建而不是创建一个新的
Activity 并将其放入堆栈中。

您可能更改了清单以添加会干扰您目标的 android:launchMode 属性。默认情况下,启动一个 Activity 会启动一个新实例。

另外:

  • 摆脱 myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);,因为您不希望基于您在此处编写的内容创建新任务,
  • 因为 context 可能是 < code>Context,我不知道你为什么要经历所有的 ContextWrapper / getBaseContext() 内容

However when I call it for a second it
just returns to the Activity already
created rather than creating a new
Activity and placing it on the stack.

You probably changed your manifest to add an android:launchMode attribute that is interfering with your goal. By default, starting an activity starts a new instance.

Also:

  • Get rid of myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);, since you do not want a new task based on what you have written here
  • Since context is probably a Context, I do not know why you are going through all of the ContextWrapper / getBaseContext() stuff
岁月打碎记忆 2024-09-15 07:29:04
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
            | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
            | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文