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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:43:34+00:00 2026-06-05T23:43:34+00:00

I have a small DB and read the required extract of data in a

  • 0

I have a small DB and read the required extract of data in a Singleton object stored statically like this in a class called MainDataManager:

public class MainDataManager {
    private static Context context;
    public static MainDataManager mainDataManager = null;

    public static MainDataManager getInstance(Context ctx) {
        context = ctx;
        if (mainDataManager == null) {  
           ...get the data from DB
          ...put the data in static mainDataManager variable 
          } 
        return mainDataManager;
        }

Now in every class/activity of the app I can access the MainDataManager.mainDataManager data easily.

At some time I would like to save the data back to the database.

Currently I do this on each activity in

 @Override
    protected void onPause() {
    super.onPause();
    saveState(); //saves the mainDataManager's data to the DB
    }

I would like to avoid this and think it is actually “save” enough if I update the database only when the user exits the app (whatever this means) – or even better when Android wants to destroy the MainDataManager object.

While I could put my saveState() call in activities in the onDestroy() method – I would prefer to only save the data to DB if the MainDataManager object gets destroyed. But there is no onDestroy() for a normal object.

What would be the best way to handle that?

ps the reason why I do not want to saveState() in each activity’s onDestroy() is, because saving to database is quite slow even with very little data (esp. on older Android phones!) and this is just not the logic – just because any activity is beeing destroyed- to then update the DB.

Many 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-05T23:43:36+00:00Added an answer on June 5, 2026 at 11:43 pm

    What would be the best way to handle that?

    The way you are presently handling it, albeit perhaps using an AsyncTask from onPause(), and only from activities where you are changing the data, and perhaps using some sort of isDirty flag to let you know if data does need to be persisted.

    The real reason you do not want to wait until onDestroy() is that onDestroy() is not guaranteed to be called.

    I would prefer to only save the data to DB if the MainDataManager object gets destroyed.

    That object will never be “destroyed” — it will live until the process is terminated.

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

Sidebar

Related Questions

I have a form that looks like so: <label for=fullpath><span class=required>*Full Path of folder
I have a small text file that I'd like to read into a scalar
Im having a small problem with my code. I have been trying to read
My client can't read iPhone's default fonts, the size is too small. I have
I have file about 4MB (which i called as big one)...this file has about
I have an expensive function that takes and returns a small amount of data
I'm building a very small gem to read an image data format and return
I realise a question like this is asked pretty frequently (I've probably read every
My ColdFusion app is required to create Word 2003 documents, with complex data like
Say I was to have a method that required a small number of objects

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.