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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:49:43+00:00 2026-05-27T18:49:43+00:00

A few days ago I’ve discovered that singleton can become anti-pattern in Android. My

  • 0

A few days ago I’ve discovered that singleton can become anti-pattern in Android. My singleton (class with private constructor and instance stored in static field) was deleted (instance was deleted despite the fact other activities were still using this singleton (via getInstance() method) so another instance had to be created ) because Activity from which it was first invoked was deleted (after invoking finish for only this one activity).

I’ve read already how this problem can be resolved however I’ve also just read “Effective Java”. There is said that “Single-element enum type is the bast way to implement a singleton”.

So now I’m wondering what would be the lifecycle of singleton created this way in Android application? Would it be the same like in case of “standard singleton implementation” so after destroying activity from which it was invoked the first time it will be destroyed (even if it used also in other activities)?

I’m not asking about proper android singleton implemenation or the singleton pattern itself (is it pattern or anti-pattern etc) but I’d like to know what be the lifecycle of such enum singleton object and when it will be destroyed.

  • 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-05-27T18:49:44+00:00Added an answer on May 27, 2026 at 6:49 pm

    In all cases, the classes you use are tied to the ClassLoader that loaded them. This is true in Java in general, not just Android. Android will isolate activities by using new ClassLoaders each time — at the least, it doesn’t promise it won’t, and it does as far as I can tell.

    Any singleton, or other class-level state, is tied to the Class which is tied to the ClassLoader. This is why your state “disappears”; it’s actually that your calling code is seeing a new Class in a new ClassLoader.

    So, your enum-based trick, or anything else along these lines, would have exactly the same behavior. You just can’t “persist” activity information this way. You can and should write to a SQLite DB. You could probably stash it in the SharedPreferences too.

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

Sidebar

Related Questions

A few days ago I asked a question that might be a little unclear.
Xcode 4.0.1 was released a few days ago, which means that I am once
When I was writing C# code a few days ago I noticed that the
I started programming in Lua few days ago. I have become familiar with the
Few days ago I've a weird idea came into my mind that manipulate if();
Few days ago I tried to create a system that jQuery will enable me
few days ago, my freind asked me to build him a proagram that start
A few days ago I was told that recent versions of g++ produce better
A few days ago I saw that for ( ; ; ) results in
I'm revising my question from a few days ago. Rewrote my query. Can someone

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.