Android - 如何创建一个活动的多个实例?
我想知道是否可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能更改了清单以添加会干扰您目标的
android:launchMode
属性。默认情况下,启动一个 Activity 会启动一个新实例。另外:
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
,因为您不希望基于您在此处编写的内容创建新任务,context
可能是 < code>Context,我不知道你为什么要经历所有的ContextWrapper
/getBaseContext()
内容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:
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
, since you do not want a new task based on what you have written herecontext
is probably aContext
, I do not know why you are going through all of theContextWrapper
/getBaseContext()
stuff