close an activity from other activity?

2019-01-20 19:19发布

问题:

Does anyone know how to close an activity from other activity?? for example: i have 3 activity (activity A, B, and C) and from activity C, i can close an activity A.. my activity structure is activity A -> activity B -> activity C how to close an activity A from activity C?

i was try this code :

@Override

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent goToLockScreen= new Intent(this,LockScreenForm.class);
        startActivity(goToLockScreen);
        finish();

but that code is only fot closing activity A from activity B, and can't close activity A from activity C direcly..

Does anyone know about closing an activity direcly from other activity?? thanks..

回答1:

First Go to parent activity by starting it
@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
switch(Code){
 case A: go to that activity and finsih() this again come back to parent activity
 case B: go to that activity and finsih() this again come back to parent activity
/////and son on
}


回答2:

try It work perfectly for me

   `public class aActivity extends Activity {

    public static Activity handleToClose;

    @Override
          public void onCreate(Bundle savedInstanceState) {
    .
    .
    .
    handleToClose = this;
    }

    public void onClick(View v)
    {
   Intent i = new Intent(this, act2.class);
   startActivity(i);
     }
    }`

Now You have to finish Activity-A from Activity-B

Activity-B or 2nd Activity

    `public class act2 extends Activity {
 public void onCreate(Bundle savedInstanceState) {
      // your code here
          }

     public void onClick(View v)
     {
    aActivity.handleToClose.finish(); //this will finish aActivity (1st Activity)
     finish();//to finish current Activity
     }
      }`


回答3:

Intent goToLockScreen= new Intent(this,LockScreenForm.class);
goToLockScreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

This is a prescribed way and you should follow it.. if you want some other behaviour yo can implement it.. there are lot many questions asked on this topic.. refer other questions...



回答4:

What about starting both B and C forResult and send a result back to pervious activity to let A finally call finish()? Like this:

A startActivityForResult() -> B startActivityForResult()-> C
C setResult()-> B onActivityResult(){setResult()} -> C onActivityResult(){finish()}

Sounds complicated, but maybe it can be used as a workaround?



回答5:

try this

If the update activity is launching another installation activity, then you may want to override void onActivityResult(int requestCode, int resultCode, Intent intent) in the update activity, providing the following implementation. Also, when the update activity launches the installation activity, it should do so with startActivityForResult(Intent, int), not with startActivity(Intent).

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
finish();
}