18

I think this is a basic question. Is there any option to stop an activity by using intent.

Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:5554"));
startActivity(intent);

This is my code. I would like to stop this activity (That means, i want to drop this call) if the user is busy or something. What can I do for that? I tried this:

if (condition) {
    Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:5554"));
    startActivity(intent);
}else {
    this.finish();
}

But of no use. Does anybody have a suggestion?

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Tony
  • 277
  • 2
  • 3
  • 10
  • 1
    Seems like it would be a security violation if one application could kill another one. – EJK Jan 19 '13 at 05:46
  • Unless of course the target application published this capability via an Intent. – EJK Jan 19 '13 at 05:54

3 Answers3

29

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(intent);
            }
        }
    });

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 = intent.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);
halfer
  • 19,824
  • 17
  • 99
  • 186
Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69
  • Thanks a lot Torcellite!! This may help me alot. – Tony Jan 19 '13 at 06:09
  • @Tony Any time, do vote it up for others to view it in case they run into this :) – Karthik Balakrishnan Jan 19 '13 at 06:10
  • @Tony Also, experiment with the code in B a little, it may not work very well unless since I don't know what exactly you want. – Karthik Balakrishnan Jan 19 '13 at 06:11
  • Nop!! I got the idea from first one itself(Code A). Actually I want to make a call by using an intent and have to cut the call if the user is busy with some another call. That's why I used StartActivity(Intent); but has no idea of how to stop it. I searched a lot for a method like stopActivity. But didn't found yet. – Tony Jan 19 '13 at 06:17
  • There is no `stopActivity`. This is the way I've figured out to kill one activity from another activity. – Karthik Balakrishnan Jan 19 '13 at 06:20
  • @ Torcellite I have not much reputation to vote it up. But sure I will guide them. Thank You!! – Tony Jan 19 '13 at 06:32
  • I believe onNewIntent() does not get called if Activity B was not already open. Furthermore, skipping initialization in onCreate() will NOT stop the activity from launching. **Therefore, in onCreate(), must have: `if(keep==false){ finish(); }`** – Jodes Aug 23 '14 at 11:31
  • @Jodes If activity B was not already open, the Dev wouldn't want to close it, would he? – Karthik Balakrishnan Aug 24 '14 at 00:16
  • But the intent will launch activity B whether or not keep==false! So while it might not have been open, the intent to close will open it, so it then needs to be closed. (Unless I'm being really stupid here) – Jodes Aug 24 '14 at 11:22
  • @Jodes - Unless the checkbox has been checked, there's no way the activity would've started. However, your case is valid. If the OP isn't using a checkbox and is using a button, then your case is valid. – Karthik Balakrishnan Aug 24 '14 at 12:26
  • @Jodes - Na, you thought of a specific case that I didn't. Kudos :) – Karthik Balakrishnan Aug 24 '14 at 15:32
  • 1
    @KarthikBalakrishnan I did an upvote because it took me on the right track. However, I do not need the mentioned manifest entry but use `android:excludeFromRecents="true"` and handle extra boolean `keep` in `onStart()` without using `onNewIntent()`. Then I use `startActivity(intent)` with intent having these flags set: `FLAG_ACTIVITY_NEW_TASK`, `FLAG_ACTIVITY_CLEAR_TASK` and `FLAG_ACTIVITY_SINGLE_TOP`. – yasd Mar 31 '18 at 12:53
0

You can turn off the background data of play store and play services for not entering the play store .it will simply say, " COULD NOT BE LOADED " Thats the only way i found to stop intent

0
This worked for me:

AndroidManifest.xml:

        <activity android:name=".ui.activities.SettingsActivity"
            android:launchMode="singleTop"
            android:excludeFromRecents="true">
            <intent-filter>
                <action android:name="android.intent.action.STOP_SERVICE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>

Intent:

Intent stopBackgroundServiceIntent = new Intent(this, SettingsActivity.class);
// Remove the activity when finish is called
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK );
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
live-love
  • 48,840
  • 22
  • 240
  • 204