活動を開始する通常の方法は、
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
オブジェクトからアクティビティを開始する方法はありますか?..のように
SecondActivity var = new SecondActivity();
var.start();
そんな感じ..?!
活動を開始する通常の方法は、
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
オブジェクトからアクティビティを開始する方法はありますか?..のように
SecondActivity var = new SecondActivity();
var.start();
そんな感じ..?!
Context を CustomObject に渡し、それを使用してアクティビティを開始するだけです。
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...
}
そして、オブジェクトを作成するアクティビティ内:
CustomObject obj = new CustomObject(this);
obj.startActivity();
私はあなたがこれを逆に持っているかもしれないと思います。あるアクティビティから別のアクティビティへ、設定データを含むオブジェクトへの参照を送信する必要があるようです。setXxxExtra()
これは、Intent クラスのメソッドを使用して行うことができます。
2 番目のアクティビティから最初のアクティビティにデータを戻すには、最初に 2 番目のアクティビティを開始してから、メインのアクティビティでstartActivityForResult()
オーバーライドする必要があります。Android 開発者ページには、これを行う方法に関する非常に良い例があります。onActivityResult()