Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9247381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:42:46+00:00 2026-06-18T09:42:46+00:00

As I understand it, an activity being destroyed is not equivalently to an activity

  • 0

As I understand it, an activity being destroyed is not equivalently to an activity being finished.

  • Finished
    • The activity is removed from the back stack.
    • It can be triggered by the program (e.g. by calling finish()), or by the user pressing the back key (which implicitly calls finish()).
    • Finishing an activity will destroy it.
  • Destroyed
    • The Android OS may destroy an invisible activity to recover memory. The activity will be recreated when the user navigates back to it.
    • The activity is destroyed and recreated when the user rotates the screen.
    • Reference: Recreating an Activity

So how do I finish a destroyed activity? The finish() method requires an Activity object, but if the activity is destroyed, I have no Activity object – I am not supposed to be holding a reference to a destroyed activity, am I?


Case study:

I have an activity a, which starts b, which in turn starts c (using Activity.startActivity()), so now the back stack is:

a → b → c

In c, the user fills out a form and tap the Submit button. A network request is made to a remote server using AsyncTask. After the task is completed, I show a toast and finish the activity by calling c.finish(). Perfect.

Now consider this scenario:

While the async task is in progress, the user switches to another app. Then, the Android OS decided to destroy all 3 activities (a, b, c) due to memory constraints. Later, the async task is completed. Now how do I finish c?

What I have tried:

  • Call c.finish():
    • Can’t, because c is destroyed.
  • Call b.finishActivity():
    • Can’t, because b is destroyed.
  • Use Context.startActivity() with FLAG_ACTIVITY_CLEAR_TOP so as to raise b to the top, thus finishing c:

    // appContext is an application context, not an activity context (which I don't have)
    Intent intent = new Intent(appContext, B.class);    // B is b's class.
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    appContext.startActivity(intent);
    
    • Failed, appContext.startActivity() throws an exception:

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Edit: Clarification: I need to wait until the async task finishes and decide whether to finish c based on server’s response.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:42:47+00:00Added an answer on June 18, 2026 at 9:42 am

    Can’t finish a destroyed activity directly, so just finish() it in its onCreate() (suggested by @Labeeb P). Here’s how:

    1. If the activity is already destroyed when trying to finish it, save a boolean flag somewhere instead.

      if(activity != null)
      {
          // Activity object still valid, so finish() now.
          activity.finish();
      }
      else
      {
          // Activity is destroyed, so save a flag.
          is_activity_pending_finish = true;
      }
      
      • If the flag needs to stay even if the app is destroyed, use persistent storage, e.g. SharedPreferences (suggested by @Labeeb P).
    2. In the activity’s onCreate(), check the flag and call finish().

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
      
          if(is_activity_pending_finish)
          {
              is_activity_pending_finish = false; // Clear the flag.
      
              // This activity should have been finished, so finish it now.
              finish();
              return;
          }
      
          ...
      }
      
      • If there’re multiple instances of the same Activity class, you may need something more than a boolean flag to identify the specific instance of activity to finish.

    Calling finish() in onCreate() is actually a legimate operation, as it is mentioned in the doc:

    … you might call finish() from within onCreate() to destroy the activity. In this case, the system immediately calls onDestroy() without calling any of the other lifecycle methods.


    Other considerations:

    • It may not be a good idea to finish an activity while the app is in background, especially if it is the only activity. Make sure that you don’t confuse the user.
    • For better user experience, if you finish an activity while the app is in background, you may want to inform the user. Consider using toasts (good for short notices) or notifications (good for long operations that the user may have forgotten)(suggested by @Stephan Branczyk and @dilix).

    Of course, an activity being destroyed doesn’t necessary mean that the app is in background (there might be another foreground activity). Still, the above solution (calling finish() in onCreate()) works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the following program force quit and crashes, I don't understand why, import android.app.Activity; import
How to prevent an activity from being recreated on turning screen off? What I
I've read many posts about reloading the Activity. I understand it is not the
I understand when an activity closes, onDestroy() is called. But this is not done
I understand that the system may kill the activity without calling onDestroy(). Say I
I understand, from MSDN, that ClassInitialize is to mark a method that will do
I understand I can create an enum like this: public enum MyEnum { ONE(1),
I understand that JVM and CLR were designed as stack-based virtual machines. When JIT
I understand how to use intents and startActivity() when opening another activity within my
I don't seem to understand if it's possible to inflate (include) an activity into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.