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

  • Home
  • SEARCH
  • 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 9197327
In Process

The Archive Base Latest Questions

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

Possible Duplicate: State of variables inside an Android activity If android OS kills my

  • 0

Possible Duplicate:
State of variables inside an Android activity

If android OS kills my application (which uses service ) due to low memory, then it will try to run it again. From android.developers site:

**Note this means that most of the time your service is running, it may be killed by the system if it is under heavy memory pressure. If this happens, the system will later try to restart the service. **

If i have in my main activity code like this:

public static MainActivity mainAct;
protected void onCreate(Bundle savedInstanceState) 
{mainAct = this;}

When android OS kills my application value of mainAct is lost, because its initialized only onCreate() in activity not in service.

How to “save” that object so it will retain value if my app is killed due to low memory?

  • 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-17T22:02:52+00:00Added an answer on June 17, 2026 at 10:02 pm

    You can implement the onDestroy() method in your service to persist some application state to the device. So, when the services onStartCommand() method is call you can check your persisted state (if it exists) and start your application again from that point.

    For example, if you have a news feed app that pulls articles from different sources you can write your state to the file before you start the download to say something like:

    download=irishtimes-31_01_2013
    

    So if your application fails completely because someone pulled the battery out then you have a known state to resume from. Likewise, if Andoid calls your onDestroy() method to terminate your service you can persist a state to the device in the same way. Then when your service starts back up you have the state that you can read from the file and load into your “object” to use again.

    You are not restricted to just files. As @wtstang02 mentioned you can also use a database.

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

Sidebar

Related Questions

Possible Duplicate: How do I save an Android application's state? I have an Android
Possible Duplicate: ToggleButton state change programmatically rather than automatically in Android? I am trying
Possible Duplicate: Android ImageButton with a selected state? I was wondering whether there is
Possible Duplicate: ASP.NET Application state vs a Static object I am using the static
Possible Duplicate: Android: Saving a state during Android lifecycle i want to save some
Possible Duplicate: Android : why PhoneCallListener still alive after activity finish? i'm making a
Possible Duplicate: What is Context in Android? i read context in android current state
Possible Duplicate: 'Operation is not valid due to the current state of the object'
Possible Duplicate: Android 4.1: How to check notifications are disabled for the application? One
Possible Duplicate: Tri-State Checkboxes in WinForms TreeView How can I change the symbol within

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.