android如何杀死另一个活动?

时间:2013-06-26 03:42:32

标签: java android android-activity

在应用程序中,有3个活动,A,B,C。 A是登录.B是菜单,C是设置值。工作线:A-> B-> C.在C中,有一个名为logout的按钮。单击logout时,我将setResult设置为B,完成C并启动A.在B中,我覆盖onActivityResult,当结果代码正确时,我结束B.但是测试数据显示当A重启C时,B未完成。欢迎解决方案!这是代码:

    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();

                    }
           }
    }

2 个答案:

答案 0 :(得分:0)

startActivity(intent);//start the next activity
finish(); //finish the current activity

找到描述性讨论here

答案 1 :(得分:0)

finish()的{​​{1}}末尾致电startActivity(<Your Intent>)。移至ActivityB时,ActivityB将不会处于堆叠状态。