11

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

Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69

4 Answers4

50

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();     
YakovL
  • 7,557
  • 12
  • 62
  • 102
Lumis
  • 21,517
  • 8
  • 63
  • 67
  • I want it to kill the app only if the checkBox is disabled. Isn't there a way to just pass a variable without using intents? – Karthik Balakrishnan Jan 16 '13 at 11:02
  • Yes, use the above getInstance, then you can access any public variable and method in the activityA. In activity B, state ActivityA activityA = ActivityA.getInstance(); Then simply check your box by if(activityA.checkBox){....} – Lumis Jan 16 '13 at 11:08
  • 2
    When you close ActivtyA, kill the reference to it in Activity B: activityA = null; – Lumis Jan 16 '13 at 11:11
  • I understand why the null pointer exception arises. The activity hadn't been called yet. – Karthik Balakrishnan Jan 16 '13 at 11:21
  • 1
    I don't think so, but it is a way to access public items of another class, which includes Activity, which still only a class. Another way to share varibales is to create a static varibales in a separate class, then all activities can use it. – Lumis Jan 16 '13 at 11:26
  • Yes, you can check if the instance is null first – Lumis Jan 16 '13 at 11:28
  • This is just a singleton approach...Not helpful – DJphy May 19 '15 at 06:18
  • Nice, Really simple . TX – ClaudioC Oct 06 '17 at 15:33
  • You should strictly avoid declaring android context objects (such as activity) as static because memory leaks could occur. – user221256 Jan 07 '18 at 18:34
  • make static ActivityA activityA -> public static ActivityA activityA – aac Apr 17 '19 at 06:05
0

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.

a b
  • 1
  • 1
  • 4
    It doesn't seem to work and I fail to understand how this could work. The code basically creates a new activity object and then calls finish on it. However, this object is not the activity object stored in the stack. Thus, the activity in the stack stays there. – omega Sep 06 '13 at 15:27
0

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 = new BroadcastReceiver() {
        @Override
        public void onReceive(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 ");
                }
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(mContext).registerReceiver(bgshowBroacast, new IntentFilter("BG_SHOW_BROADCAST"));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(mContext).unregisterReceiver(bgshowBroacast);
    }

Then in Activity_B (in my case Bg_send Class) :

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

Thus the previous activity will finish where the receiver registered.

Happy coding.... .

Noor Hossain
  • 1,620
  • 1
  • 18
  • 25
-1

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,