-1

I am wondering if there's a stopActivity() function like the stopService() function.

My research indicates that there is no such function. However, I still need the ability to close one Activity from another.

How can I accomplish this?

4

2 回答 2

0

I had this problem a few days ago, and I'm happy to tell you I've found a way around this.

First of all, to the activity you want to stop add this in the AndroidManifest.xml:

android:launchMode="singleTop"

I'm going to use a CheckBox example. When it's checked the activity is started and when unchecked will kill the activity.

Example Activity A is calling Activity B and then killing it using an intent.

Code to be put in A:

checkbox.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(A.this, B.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            if (enable.isChecked()) {
                intent.putExtra("keep", true);
                startActivity(intent);
            }
            else
            {
                intent.putExtra("keep", false);
                startActivity(filterAct);
            }
        }
    });

Code to be put into B:

boolean keep;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.B);
    intent = this.getIntent();
    boolean keep = brightnessIntent.getExtras().getBoolean("keep");
    if(keep==true)
    {
        //execute your code here

    }
 }
    @Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    keep = intent.getExtras().getBoolean("keep");
    if(keep==false)
    {
        B.this.finish();
            }
}
}

Explanation : What this basically does is, when the checkbox is checked it calls the activity and passes a boolean value, if it's true the activity is kept alive and is brought to the foreground. Now, if you don't pass the flag singleTop then many instances of this activity will be created. singleTop makes sure only the same instance is called. Now, when the checkbox is unchecked a new value for keep is passed which is verified in B. If unchecked, the Activity A will be passing false, and hence B terminates itself from within the onNewIntent() function.

P.S - You can close Activity B from another Activity too. Just use If the other activity is C:

   Intent intent = new Intent(C.this, B.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                intent.putExtra("keep", false);
                startActivity(intent);
于 2013-01-19T07:50:49.117 回答
0

You can use the other answer to kill an Activity, but the reason that it is not straight forward is they really want you to follow the activity lifecycle.

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

In your onPause() implementation, you are supposed to quiesce any long running processing associated with the Activity unless there is a good reason to keep it going.

Android keeps the Activity around unless it needs the memory since it is far faster to reuse an already instantiated Activity than it is to instantiate a new one. It's better to allow Android to manage the memory for you than to just kill activities yourself.

于 2013-01-19T08:06:28.230 回答