访问父活动的实例?

发布于 2024-10-17 00:37:38 字数 143 浏览 5 评论 0原文

假设我有一个类first.java(活动类),并且我在该类中启动另一个活动(Second.java - 活动类)。

如何从 secondary.java 访问first.java 的实例?

有人能给我一个很好的解释吗...一个例子就很好了...

Suppose I have a class first.java (activity class) and I start another activity in this class (second.java - activity class).

How can I access the instance of first.java from second.java?

Can someone give me a good explanation on this... An example would be great...

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

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

发布评论

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

评论(5

比忠 2024-10-24 00:37:38

如果您需要第二个活动将一些数据返回到第一个活动,我建议您使用 startActivityForResult() 来启动第二个活动。然后在第一个活动的 onResult() 中,您可以完成所需的工作。

在启动 Second.java 的 First.java 中:

Intent intent = new Intent(this, Second.class);
int requestCode = 1; // Or some number you choose
startActivityForResult(intent, requestCode);

结果方法:

protected void onActivityResult (int requestCode, int resultCode, Intent data) {
  // Collect data from the intent and use it
  String value = data.getString("someValue");
}

在 Second.java 中:

Intent intent = new Intent();
intent.putExtra("someValue", "data");
setResult(RESULT_OK, intent);
finish();

如果您不希望在第一个活动中执行某些操作之前等待第二个活动结束,您可以发送一个广播,其中第一个活动做出反应。

If you need your second activity to return some data to your first activity I recommend you use startActivityForResult() to start your second activity. Then in onResult() in your first activity you can do the work needed.

In First.java where you start Second.java:

Intent intent = new Intent(this, Second.class);
int requestCode = 1; // Or some number you choose
startActivityForResult(intent, requestCode);

The result method:

protected void onActivityResult (int requestCode, int resultCode, Intent data) {
  // Collect data from the intent and use it
  String value = data.getString("someValue");
}

In Second.java:

Intent intent = new Intent();
intent.putExtra("someValue", "data");
setResult(RESULT_OK, intent);
finish();

If you do not wish to wait for the Second activity to end before you do some work in the First activity, you could instead send a broadcast which the First activity reacts to.

眼泪淡了忧伤 2024-10-24 00:37:38

您只需从子活动中调用 getParent() 即可。

我不知道为什么其他答案如此复杂。

You can simply call getParent() from the child activity.

I have no clue why other answers are so complicated.

思念满溢 2024-10-24 00:37:38

只有这个才应该

class first
{
    public static first instance;
    oncreate()
    {
        instance = this;
    }
}

首先起作用。instance是可以从第二个类访问的必需的东西

Only this should work

class first
{
    public static first instance;
    oncreate()
    {
        instance = this;
    }
}

first.instance is the required thing that is accessible from the second class

薄暮涼年 2024-10-24 00:37:38

试试这个,如果这个有效的话 4 u.........
像这样......

class first
{
public static first instance;
oncreate()
{
instance=this;
}

public static getInstance()
{
return instance;
}

}

现在从第二堂课调用first.getInstance();

您也可以像这样以静态方式直接访问实例。first.instance.......
谢谢...

try this if this work 4 u.........
something like this.....

class first
{
public static first instance;
oncreate()
{
instance=this;
}

public static getInstance()
{
return instance;
}

}

now from second class call first.getInstance();

you can also directly acess instance in static way like this first.instance.......
Thanks...

浅忆 2024-10-24 00:37:38

您无法直接创建活动。
在第一个活动中采用这样的静态活动变量,

public static Activity activity;

在 onCreate 中执行此操作。

activity = this;

然后在第二个活动中执行此操作,

Activity activity = (your activity name).activity;

编辑:
对于将数据从一个活动传递到另一活动来说,这不是一种方法。
上面的答案是从最初询问的其他活动中获取活动实例。

要将数据从一个活动传递到另一活动通常使用捆绑包。但如果数据不是原始数据类型,则使用应实现 Parcelable 或 Serialized 接口的对象类。然后通过bundle我们只能传递可分割的对象列表。

You can't create an activity directly.
In the first activity take a static activity variable like this,

public static Activity activity;

In the onCreate do this.

activity = this;

Then in the second activity do this,

Activity activity = (your activity name).activity;

Edit:
For passing data from one activity to other activity this is not the way.
Above answer was to get activity instance from other activity which was initially asked.

To pass data from one activity to other activty generally use bundle. But if the data is not primitive data type, then use object class which should implement parcelable or serializable interface. Then through bundle only parcelable list of objects we can pass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文