gpt4 book ai didi

java - 从对象启动 Activity

转载 作者:行者123 更新时间:2023-11-29 05:39:15 26 4
gpt4 key购买 nike

开始 Activity 的通常方式是

Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);

有没有办法从一个对象启动一个 Activity?...比如

SecondActivity var = new SecondActivity();
var.start();

类似的东西..?!

最佳答案

只需将 Context 传递给您的 CustomObject 并使用它启动 Activity:

public class CustomObject {

Context c;
// and some other fields here...

public CustomObject(Context c) {
this.c = c;
}

public void startActivity() {

Intent intent = new Intent(c, SecondActivity.class);
c.startActivity(intent);
}

// and some other methods here...
}

在创建对象的 Activity 中:

CustomObject obj = new CustomObject(this);
obj.startActivity();

关于java - 从对象启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18291960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com