Android的 - 如何创建活动的多个实例? [英] Android - How to create Multiple instances of an Activity?

查看:144
本文介绍了Android的 - 如何创建活动的多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能在Android中创建一个活动的多个实例?

我目前使用下面的code开始自己的inCall屏幕用于VoIP测试:


 公共无效initInCallScreen(字符串PNAME,字符串phoneNumber的,诠释的ContactID,布尔
        callDirection,诠释另一LineID){

    //开始在callScreen对话框
    最终意图myIntent =新的意图(背景下,CallDialogActivity.class);
    myIntent.putExtra(名,PNAME);
    myIntent.putExtra(编号,phoneNumber的);
    myIntent.putExtra(ID,的ContactID);
    myIntent.putExtra(CALLTYPE,callDirection); //真=传入,假=传出
    myIntent.putExtra(另一LineID,另一LineID);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);
 


这让我开始活动就好了。

然而,当我把它一秒钟它只是返回到已创建的,而不是创建一个新的活动,并把它在堆栈上的活动。

我希望能够创建活动多次,让我有堆栈上的两个或3活动,用户可以在它们之间进行切换,使用主页,后退按钮等等...

这是可能的,如果是这样我在做什么错了?

解决方案
  

当我把它一秒钟它不过   刚刚返回到活动已经   创建而不是创建一个新的   活动,并把它压入堆栈。

您可能改变了你的清单中添加一个安卓launchMode 属性,它干扰了你的目标。缺省情况下,启动一个活动开始的新实例。

另外:

  • 摆脱 myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ,因为你根据你在这里写不想要一个新的任务
  • 由于上下文可能是一个上下文,我不知道为什么你要通过所有的 ContextWrapper / getBaseContext()的东西

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?

解决方案

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

这篇关于Android的 - 如何创建活动的多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆