-1

In an app,there are 3 activities,A,B,C. A is to log in .B is the menu,C is to set values. The working line: A->B->C. In C, there is a button named logout. When clicking logout , I setResult to B ,finish C and start A. In B,I override onActivityResult, when the resultcode is right, I finish B. But the testing data shows when A is restarted by C,B isn't be finished. Solutions are welcomed! Here is the code:

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

                    }
           }
    }
LilySea
  • 11
  • 2

2 Answers2

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

Found a descriptive discussion here

Community
  • 1
  • 1
  • for the sake of the menu function, ActivityB cannot be finished when moving to ActivityC. In ActivityC, there is other buttons to do other work. – LilySea Jun 26 '13 at 09:05
0

Call finish() at the end of the startActivity(<Your Intent>) in ActivityB. Then ActivityB will not be in stack when moving to ActivityC.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
  • for the sake of the menu function, ActivityB cannot be finished when moving to ActivityC. In ActivityC, there is other buttons to do other work. – LilySea Jun 26 '13 at 07:02
  • B activity is the menu. In C , I would back to B. As a result, I can't destroy B when I move to C. Could you provide another solution? Thank you so much. – LilySea Jun 26 '13 at 09:08
  • Okay. I too faced the same problem. Please add this line to your `activity` in `AndroidManifest.xml`: `android:launchMode="singleTask"` – Avadhani Y Jun 26 '13 at 09:11
  • @LilySea do u find the solution?? – Avadhani Y Jun 26 '13 at 12:47
  • your advice doesn't work either.I just do it as below and it works. private ActivityB intance=null; in onCreate(), instance=this; in ActivityC, B.instance.finish(). – LilySea Jun 27 '13 at 03:49