安卓:可以开始在同一个任务的活动的多个实例? [英] Android: Possible to start multiple instances of an Activity in same task?

查看:69
本文介绍了安卓:可以开始在同一个任务的活动的多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用这个code启动多个活动从父活动:

I tried using this code to start multiple Activities from a parent activity:

for (int i=0; i<NUM_ACTIVITIES; i++) 
{
    Intent intent = new Intent(this, MyActivity.class);
    startActivity(intent);
}

不过,根据我的日志 MyActivity.onCreate(),只有1活动实际创建。难道这种行为正常吗?如果是这样,什么是正确的方法来启动多个活动?

However, according to my log in MyActivity.onCreate(), only 1 Activity was actually created. Is this behavior expected? If so, what's the proper way to launch multiple Activities?

推荐答案

您不能在同一时间对多张活动。你们是不是有他们才能运行,一前一后?

You can't have multiple activities on top at the same time. Are you trying to have them run in order, one after the other?

要做到这一点的方法之一是启动每个活动的结果是:

One way to accomplish this is to start each activity for result:

Intent intent = new Intent(this, MyActivity.class);
startActivityForResult(intent, 0);

如果您使用请求code当活动运行跟踪。然后,在onActivityResult你就可以开始下一个:

Where you use the request code to track when activity is running. Then, in onActivityResult you can start the next one:

protected void  onActivityResult  (int requestCode, int resultCode, Intent  data) {
  if (requestCode < NUM_ACTIVITIES) {
    Intent intent = new Intent(this, MyActivity.class);
    startActivityForResult(intent, requestCode + 1);
  }
}

编辑: 如果你想有一些活动在后台立刻,您可以通过在每个活动的onCreate调用startActivity把它们结合在一起。如果创建任何意见之前开始的onCreate一个新的活动,该活动将永远是可见的。

If you want to have some of the activities immediatly in the background, you can chain them together by calling startActivity in each Activity's onCreate. If you start a new Activity in onCreate before creating any views, the activity will never be visible.

protected void  onCreate  (Bundle savedInstanceState) {
  int numLeft = getIntent().getIntExtra("numLeft");
  if (numLeft > 0) {
    Intent intent = new Intent(this, MyActivity.class);
    intent.putExtra("numLeft", numLeft - 1);
    startActivity(intent);
  }
}

这应该做到你想要的堆栈。

This should accomplish the stack that you wanted..

这篇关于安卓:可以开始在同一个任务的活动的多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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