在一個應用程序中,有3個活動,A,B,C。 A是登錄.B是菜單,C是設置值。工作路線:A-> B-> C。在C中,有一個名爲logout的按鈕。當單擊註銷時,我將setResult設置爲B,完成C並啓動A.在B中,我重寫了onActivityResult,當結果代碼正確時,我完成了B.但測試數據顯示A何時由C重新啓動,B未完成。歡迎解決方案!下面是代碼:android如何殺死另一個活動?
Activity A: LoginActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
login=(Button) findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(LoginActivity.this,MenuActivity.class);
startActivity(intent);
LoginActivity.this.finish();
}
}
}
Activity B: MenuActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
setting=(Button) findViewById(R.id.login);
setting.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent settingintent=new Intent(MenuActivity.this, SettingActivity.class);
startActivityForResult(settingintent,1);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==1){
switch(resultCode){
case RESULT_CANCLED:
break;
case RESULT_OK:
MenuActivity.this.finish();
Log.i(TAG, "I'm killed"+System.currentTimeMillis());
break;
}
}
}
Activity C: Setting.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
logout=(Button) findViewById(R.id.login);
logout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent=new Intent(SettingActivity.this,LoginActivity.class);
startActivity(mIntent);
SettingActivity.this.setResult(RESULT_OK);
SettingActivity.this.finish();
}
}
}
在開始下一個活動之前,完成活動。 – 323go