Killing one activity from another

2019-02-10 10:06发布

问题:

I have two activities A and B. B is a transparent pass through activity, and A is seen. I want to kill B by pressing a button A.

Here's what I've tried so far:

B obj=new B();
obj.finish();

I created an object of B and tried to kill it. That didn't work.

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("keep", true);
                startActivity(intent);

What this code is supposed to do is clear the top most activity, which is B and call B again, except this time I'm passing a value such that B kills itself after a few seconds.

This only piled up more instances of the activity for some reason. Or at least I think that's what happened because the screen became pixelated due to many transparent activities.

Here's my manifest:

<activity
        android:name="com.xxx.xxx.B"
        android:excludeFromRecents="true"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:clearTaskOnLaunch="true" >
    </activity>

What do I have to do so that, when I hit a button once the activity is displayed and the second time kills it? The creation part is obviously taken care of. My activity B pops up, I want to kill it now that B is on top.

EDIT

I tried this with a checkBox, here's the code:

enable.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            finishActivity(0);
            Intent intent = new Intent(A.this, B.class);
            if (enable.isChecked()) {
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("keep", true);
                intent.putExtra("value", 10);
                startActivityForResult(intent, 0);
            }
            else
            {
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("keep", false);
                startActivityForResult(intent, 0);
            }
        }
    });

When enable is checked the activity is called, which works fine. But they keep piling on. It's not like this A->B->A->B when I check and uncheck the checkBox. It's A->B->BB->BBB

回答1:

You could try to directly kill an activity by calling a static method in that activity:

Activity A should have a variable

 static ActivityA activityA;

In onCreate state:

 activityA = this;

and add this method:

public static ActivityA getInstance(){
   return   activityA;
 }

In activity B, call the function getInstance()

ActivityA.getInstance().finish();     


回答2:

You can stop the other activity by calling Activity.finish(). Alternatively, you can use Activity.finishActivity() to finish an activity started by startActivityForResult().



回答3:

I found a nice way to finish one activity from another, it is similar to what Lumis did. So if you want to close ActivityA from ActivityB you can do this:

In ActivityA do:

className = this.getClass().getName();

and pass it on to AvtivityB. Then in ActivityB do:

((Activity) Class.forName(className).newInstance()).finish();

You can put a string with the name of your class into className yourself, but it needs to be a full name with package too.



回答4:

Create static Class variable to save the instance: static SampleActivity sampleActivity;

On Create of first Activity save the Instance, like this: incidenteActivity = this;

Create a static method to get the instance:

public static SampleActivity getInstance(){
    return   sampleActivity;
}

wherever you want call:

SampleActivity.getInstance().finish();

it really works, regards,