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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:20:38+00:00 2026-06-08T14:20:38+00:00

Possible Duplicate: variables retaining values after app close I am relatively new to android

  • 0

Possible Duplicate:
variables retaining values after app close

I am relatively new to android and java, so I am sorry if this a stupid question. I have an android application that allows a user to take pictures and save them. I am saving the pictures as Attachment 0, Attachment 1 etc.

I am using an integer to decide the attachment number and this int gets incremented every time an attachment is created. my photos are getting saved properly with the proper names, but when I close my application the value of the integer gets reset to 0 and the already saved attachments get overwritten. How do I prevent the integer from resetting on closing and opening the application ? 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-08T14:20:40+00:00Added an answer on June 8, 2026 at 2:20 pm

    Use SharedPreferences to save the int value, then reload it in onCreate.

    There is an excellent bit of code here for you to learn from, and I’ll give you a bit of pseudocode to get you going:

    onCreate:
        prefs = <load shared preferences>;
        yourInt = prefs.getInt("name of your int", 0);
        // do whatever with yourInt
    
    onPause: // maybe not onPause, maybe you want to save each time it changes?
        prefs = <load shared preferences>;
        editor = prefs.edit();
        editor.putInt("name of your int", yourInt);
        editor.commit();
    

    And that should do it! That’s the basic concept of SharedPreferences, and from that, I think you’ll have no problem adapting the tutorials above to your purpose.

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

Sidebar

Related Questions

Possible Duplicate: Global variables in Java How to create a list of global variables
Possible Duplicate: Why global and static variables are initialized to their default values? What
Possible Duplicate: JavaScript Pass Variables Through Reference How can you do something like this
Possible Duplicate: How do I use PHP variables as values for the <script> tag
Possible Duplicate: Access shell environment variables Java I've created a stand-alone java application in
Possible Duplicate: Static Variables in WCF So I am fairly new to using WCF
Possible Duplicate: How do I set environment variables from Java? I'm trying to set
Possible Duplicate: Uninitialized variables and members in Java Why are local variables not initialized
Possible Duplicate: Why are only final variables accessible in anonymous class? This is the
Possible Duplicate: Array to named variables How can I convert an array like this

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.