Finish sub activities programmatically

I have an activity that launches another activity with startActivityForResult method. I would like to terminate the called one programmatically but I don't know how to do this since in onActivityResult() method I have no information about the called activity and I cannot call finish() on it. How can I achieve this?



Try finishActivity(requestCode). According to the documentation it lets you finish an activity previously started with startActivityForResult. And if there are multiple such activities with the same request code, all will be finished.

Note: I haven't actually tried this myself, but that's what the documentation says! Experiment with that, see if it does what you want.


The launched Activity can finish self:

setResult(RESULT_OK); finish();


At the moment you call startActivityForResult your Activity will be closed or paused and the new activity is started. The only one that can finish the new activity is the new activity.

You could start a background task and let this background task somehow notify your activity the activity could now finish itself.

I don't know if a Handler created in Activity A and passed to a thread will remain valid if Activity A is paused and Activity B is active. But I would assume this works because both of the Activities are running in the same thread therefore they should share the same message queue.


This thing just bit me, so I thought I'll add a comment here:

if(readyToFinish()){ finish() } thisCodeWillBeExecuted()

My experience is that all your code in the stacktrace gets executed, before the activity is finished. The documentation is not ideal at this point.


  • Google plus login from one activity and logout from second activity by clicking the logout button in
  • Very bad camera image quality
  • Remove an clicked item from listview android
  • Android Contact Phone Numbers fetching Duplication
  • Finish sub activities programmatically
  • Java android, getting image from gallery and show it on screen (error)
  • How can I do some cleanup right before an Android app is quitting?
  • What's the best way to have reusable dialog boxes?
  • How to upload all gallery images to a server in Android ?
  • Saving image file path to sqlite database
  • How to remove fragment from fragment manager when activity is restored after being destroyed
  • Can someone please explain how this implementation of bucket sort works?
  • Where to put clearQueue in jQuery code
  • WooCommerce hook after order is updated?
  • Return to second to last URL in MVC (return View with previous filter conditions applied)?
  • Wait for .each() .getJSON request to finish before executing a callback
  • How can I replace the server in Web Component Tester
  • Do query loads all the data in memory
  • Clear activity stack before launching another activity
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • Record samples being played with OpenAL
  • Extracting HTML between tags
  • Repeat a vertical line on every page in Report Builder / SSRS
  • QLineEdit password safety
  • Android screen density dpi vs ppi
  • Bug in WPF DataGrid
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Validaiting emails with Net.Mail MailAddress
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • How can I use Kendo UI with Razor?
  • Javascript + PHP Encryption with pidCrypt
  • Symfony2: How to get request parameter
  • log4net write single file for each call to log.info
  • Error creating VM instance in Google Compute Engine
  • Acquiring multiple attributes from .xml file in c#
  • Getting error when using KSoap library to consume .NET web services
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal