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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:26:36+00:00 2026-06-12T06:26:36+00:00

i have an Android application in which i use a class to store static

  • 0

i have an Android application in which i use a class to store static data among activities, something like:

class Global
{
      private static boolean mInitialized = false;
  private static String  mData        = null;

  public static void init()
  {
    mData        = "something";
    mInitialized = true;
  }

      public static boolean isInitialized()
      {
            return mInitialized;
      }

  public static String getData()
  {
    return mData;
  }
}

So in the main activity onCreate i do:

if( Global.isInitialized() == false )
    Global.init();

And then starts other activities, the action flow is:

MainActivity -> ActionActivity -> PluginActivity

Where Main is where i init the Global class, and Action & Plugin is where i use the getData() method of that class.

Now in some cases, i get really strange behaviour … if something unepected happens in PluginActivity ( NullPointerException for instance ), the activity crashes and the application goes back to the ActionActivity which launched it, but, at this point, during the onCreate of the ActionActivity ( where the Global class is supposed to be initialized ) i get an exception because the getData() returns null ( and isInitialized() is false ) as the Global class was never initialized by the MainActivity.

So, can an object with static members like my Global class be deallocated/cleared/whatever if something like an unexpected exception occurs ?

  • 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-12T06:26:38+00:00Added an answer on June 12, 2026 at 6:26 am

    In general, activities should be independent of each other. You should not depend on them being launched in any particular order, or at all.

    Instead, if you need to share global state data between activities, i.e. not just parameter passing in intent extras or results via onActivityResult(), subclass Application, put the init code in its onCreate() and access it from activities using getApplication(). Also remember to declare the application class in your manifest. The system takes care that the application object is there when any of your activities are running.

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

Sidebar

Related Questions

I have a class which extends android.app.Application , which I use to persist global
I would like to develop an Android application which will have a local database.
I have an android application which in its main activity, data are adapted from
I have a class which extends Application in an Android tabHost app. In the
I have an Android application, which makes use of an AlarmManager to schedule events.
I'm a student new to android, and I have an application which use background
In my Android application, I have a DefaultApplication class which extends android.app.Application , and
I have an Android application which has four tabs (I use a main TabActivity
I have a Android Application which is basically uses WebView for all interaction.. How
I have an android application in which I have to execute some tasks at

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.