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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:36:24+00:00 2026-05-16T22:36:24+00:00

I have a singleton which stores some prudent information about the user of my

  • 0

I have a singleton which stores some prudent information about the user of my application. At the moment, it stores the user’s login and the user’s location.

1)
The location is found via a Service. At the moment, the Service references my singleton directly to stuff the longitude and latitude into it. I would like to use a BroadcastReceiver to send a broadcast that the singleton hears and uses to update the values, instead.

However, to register the BroadcastReceiver, I need a Context in my singleton. What is the slickest way to achieve what I’m wanting. Is BroadcastReceiver possibly not the appropriate object?

2)
Also, what problems am I looking at with using a singleton? I assume that Android will possibly reclaim this memory at any given time (which would obviously be bad); so how can I prevent that? Would passing in the application’s Context and storing it in a member variable thwart this?

The Android documentation states: “But, the life cycle of a static is not well under your control; so to abide by the life-cycle model, the application class should initiate and tear down these static objects in the onCreate() and onTerminate() methods of the Application Class,” but I’m not entirely sure how to accomplish this.

  • 1 1 Answer
  • 2 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-05-16T22:36:24+00:00Added an answer on May 16, 2026 at 10:36 pm

    However, to register the BroadcastReceiver, I need a Context in my singleton. What is the
    slickest way to achieve what I’m wanting. Is BroadcastReceiver possibly not the appropriate > object?

    The “slickest way” is to not do what you are doing. Please only register a BroadcastReceiver from an Activity, Service, or maybe an Application. You must unregister this BroadcastReceiver when the Activity, Service, or Application is destroyed.

    I assume that Android will possibly reclaim this memory at any given time (which would
    obviously be bad); so how can I prevent that?

    You don’t. Android reserves the right to terminate your process at any time (e.g., to reclaim memory). Task killers on Android 2.1 and previous will terminate your process at any time. Once all components of your application are destroyed, Android may recycle your process at any time, clearing out your heap at the same time. And so on.

    Only put things in memory that you don’t mind losing.

    It is best to think of your “application” as a basket of loosely-coupled components, not as a monolithic entity.

    Would passing in the application’s Context and storing it in a member variable thwart
    this?

    No.

    The Android documentation states: “But, the life cycle of a static is not well under your
    control; so to abide by the life-cycle model, the application class should initiate and
    tear down these static objects in the onCreate() and onTerminate() methods of the
    Application Class,” but I’m not entirely sure how to accomplish this.

    Create a subclass of Application and indicate in the manifest that Android should use it, via the android:name attribute on the <application> element.

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

Sidebar

Related Questions

I have an application that contains LoginActivity which makes WebService request to authorize user.
Basically I have a Login window which should close once the user logs in
In my iOS application I have a set of singleton objects which are created
I have a Singleton which has an NSMutableArray containing a Class, the Class contains
I have a singleton class which is being used throughout the app. I am
I have developed an utility class for spring which is a singleton which will
I have a singleton as follows, which creates an instance of NSDictionary to hold
I have a class XYZ which extends Thread and it is also a singleton
I have a class which, by design, must follow the singleton pattern. So I
I have a Singleton to manage some variables I need in various places in

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.