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 8250501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:54:43+00:00 2026-06-07T23:54:43+00:00

I have a little problem about needing Activity/Application everywhere… More precisely I have classes

  • 0

I have a little problem about needing Activity/Application everywhere…
More precisely I have classes defined like this:

public class MyApp extends Application {

     private static Activity currentActivity;
     private static MyApp instance;

     @Override
     public void onCreate() {
         super.onCreate();
         instance = this;
     }

     public static void setCurrentActivity(Activity activity) {
         currentActivity = activity;
     }
}

And I have an Extended activity like this:

public class MyActivity extends Activity {

       ....

       @Override
       public void onResume() {
          super.onResume();
          MyApp.setCurrentActivity(this);
       }


       ....

       @Override
       public void onDestroy() {
          super.onDestroy();   
          MyApp.setCurrentActivity(null);  // for nullifying static reference

       }
 }

My questions are:

  1. Is the above code snippet fair ?
  2. Should I use super.onDestroy(); after the MyApp.setCurrentActivity(null); to verify that activity is not reffered and destroyed succesfully?
  3. And most important, what about static reference instance? where can I nullify it to not prevent from GC ?

Thanks.

  • 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-07T23:54:45+00:00Added an answer on June 7, 2026 at 11:54 pm
    1. Is the above code snippet fair ?

    Nope. The biggest problem is in MyActivity.onDestroy(). This can be called at any time when the activity is no longer in use (ie: after it has finished). If you null out currentActivity in onDestroy() you will not be clearing the reference to MyActivity, you will be clearing the reference to whatever activity happens to be currently active at the moment. I you really want to have currentActivity point to the current activity, the best you can do is something like this:
    @Override
    public void onPause() {
    super.onPause();
    if (MyApp.getCurrentActivity() == this) {
    MyApp.setCurrentActivity(null);
    }
    }

    Also, be aware that there is a small window between onPause() of an activity and onResume() of the next activity so that it is possible that currentActivity will be null during this time. Watch out for that!

    Should I use super.onDestroy(); after the
    MyApp.setCurrentActivity(null); to verify that activity is not
    reffered and destroyed succesfully?

    See my comment above. You cannot null out this reference in onDestroy().

    And most important, what about static reference instance? where can I
    nullify it to not prevent from GC ?

    You don’t need to. There is only ever one instance of MyApp created and it lives for as long as your process exists. When Android doesn’t need your process anymore it just kills the process, which will clean up everything automagically.

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

Sidebar

Related Questions

I have a little problem with my if statement when date.name is about this
I have a little problem understanding XMLHttpRequest 's handlers. The specification says this about
I have a little problem about the sort direction of a specific column in
I have a little problem about Google Maps. I'm using Geocoding (xml) for getting
I have a little problem about using jQuery (I really do not know jQuery
I am little bit confused about following problem & their solutions: i have 2
I have a little problem. This is my code: stackPanelShapesContainer = new StackPanel(); InitializeStackPanle();
i have a little problem about preg_match_all and str_replace <? $source = 'hello @user_name,
I have a fun little problem where I'm presented with a logic clause like
I have to solve this little problem in Python/Django and I'm wondering which is

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.