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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:35:17+00:00 2026-06-11T22:35:17+00:00

At first I have strong Java knowledege, but however just started with Android. My

  • 0

At first I have strong Java knowledege, but however just started with Android.

My Android app is downloading some fairly complex data (texts, dates, images) which I am saving in a custom object. The data need to be refresh from time to time. However usually the data downloaded will not change.

In order to keep the data in memory I am using the Application Object. Unfortunately, it looks like the application object instance is destroyed when the app is killed.

Hence, I was wondering if it would be of good practice to serialize and save my custom object (which is contained in the application object) in the internal storage during onPause(). Obviously, I would then first read from the file in onResume() before reloading from the internet. The idea is also to enable offline viewing.

In longer term the plan is to move the code downloading the date in a background service. As there seems to be many different ways to keep application state in Android, I would like to be be sure that this is the correct way to go.

  • 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-11T22:35:18+00:00Added an answer on June 11, 2026 at 10:35 pm

    Try using those methods class to save the Object(s) (implements serialize) you need:

    public synchronized boolean save(String fileName, Object objToSave)
        {
            try
            {
    
                // save to file
                File file = new File(CONTEXT.getDir("filesdir", Context.MODE_PRIVATE) + "/file.file");
                if (file.exists())
                {
                    file.delete();
                }
    
                file.getParentFile().mkdirs();
                file.createNewFile();
    
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
                oos.writeObject(objToSave);
                oos.close();
    
                return true;
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
                return false;
            }
            catch (IOException e)
            {
                e.printStackTrace();
                return false;
            }
        }
    
    
    public synchronized Object load(String fileName)
        {
            try
            {
    
                File file = new File(CONTEXT.getDir("filesdir", Context.MODE_PRIVATE) + "/file.file");
                if (!file.exists())
                {
                    return null;
                }
    
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
                savedObj = ois.readObject();
                ois.close();
    
                return savedObj;
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
                return null;
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return null;
            }
        }
    

    You’ll need to cast the Object you load().
    CONTEXT is an Activity or ApplicationContext to get access to the cachedir.
    Your could use Environment.getExternalStorageState() instead to get a directory path. DOn’t forget to add it “/filename”.

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

Sidebar

Related Questions

I have a string like this: |T1| This is some text for the first
I just started learning Java and I'm having trouble formatting string. In the problem
I have read Java String class is immutable and thread-safe but I am still
first of all, i am quite new to Ruby, although i have a strong
Possible Duplicate: Capitalize First Char of Each Word in a String Java I have
How do I tell that two source-codes (independent of their language C,Java,Lisp...) have strong
I have a string with the contents of the first line in a script,
goal: I have the string 1234432144 I want to only replace the first 2
In my code i have a string and i want to capitalize first letter
I have the following String First Last <first.last@email.com> I would like to extract first.last

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.