我創建了3個活動。活動一個開始的活動B的結果,並依次活動B簡單地開始活動C和自我調用finish()。活動C在啓動後,將結果設置爲任何想要的自動完成。活動A中的onActivityResult被調用,所以我相信答案是YES,它可以完成。
這裏是我的樣品活動 活動A:
public class MainTestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent in = new Intent(MainTestActivity.this, TestActivityB.class);
startActivityForResult(in, 101);
}
}, 2000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_test, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this, "Got Intent", Toast.LENGTH_SHORT).show();
}
}
活性B:
public class TestActivityB extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent in = new Intent(TestActivityB.this, TestActivityC.class);
startActivity(in);
finish();
}
}, 2000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_test, menu);
return true;
}
}
活性C:
public class TestActivityC extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
setResult(101);
finish();
}
}, 2000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_test, menu);
return true;
}
}
我甚至在Activity B中評論過'finish()'。當我通過按下後退按鈕銷燬活動B時,活動A能夠顯示吐司。 – Rajeev
不是我想要做的,但它確實有效。很好的回答,謝謝。 –