Skip to content Skip to sidebar Skip to footer

Killing One Activity From Another

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.f

Solution 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:

publicstatic ActivityA getInstance(){
   return   activityA;
 }

In activity B, call the function getInstance()

ActivityA.getInstance().finish();     

Solution 2:

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.

Solution 3:

in my opinion, the clearer approach is create a local broadcast, in this way no memory leak or null problem. Actually this is the exact and proficient way for passing data to previous activity. First, create a reciever in Activity_A and register it on resume and uregister it on destroy. In Activi_A (in my case Bg_show Class) :

BroadcastReceiver bgshowBroacast = newBroadcastReceiver() {
        @OverridepublicvoidonReceive(Context context, Intent intent) {
            String extra = intent.getStringExtra("BROADCAST");
            if (extra != null) {
                if (extra.equalsIgnoreCase("finishBgShowActivity")) {

                    finish();
                    Log.i(TAG, "onReceive: Bg_show_BroadCast receive from bg_send class ");
                }
            }
        }
    };

    @OverrideprotectedvoidonResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(mContext).registerReceiver(bgshowBroacast, newIntentFilter("BG_SHOW_BROADCAST"));
    }

    @OverrideprotectedvoidonDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(mContext).unregisterReceiver(bgshowBroacast);
    }

Then in Activity_B (in my case Bg_send Class) :

Intentintent=newIntent("BG_SHOW_BROADCAST");
                        intent.putExtra("BROADCAST", "finishBgShowActivity");
                        LocalBroadcastManager.getInstance(mContext)
                                .sendBroadcast(intent);

Thus the previous activity will finish where the receiver registered.

Happy coding.... .

Solution 4:

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

Solution 5:

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:

publicstatic SampleActivity getInstance(){
    return   sampleActivity;
}

wherever you want call:

SampleActivity.getInstance().finish();

it really works, regards,

Post a Comment for "Killing One Activity From Another"