开始活动的常用方法是
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
有没有办法从对象启动一个Activity?..比如
SecondActivity var = new SecondActivity();
var.start();
那样的事情......?!
答案 0 :(得分:2)
只需将上下文传递给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();
答案 1 :(得分:0)
我想你可能会倒退。听起来你应该发送对包含你的偏好数据的对象的引用,从一个Activity到另一个Activity。您可以使用Intent类中的setXxxExtra()
方法执行此操作。
要将数据从第二个Activity发送回第一个Activity,您应首先使用startActivityForResult()
启动第二个Activity,然后在主Activity中覆盖onActivityResult()
。 The Android developer pages有a very good example about how to do this。