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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:13:22+00:00 2026-06-17T15:13:22+00:00

In my application, I extend the application class to maintain information relevant to the

  • 0

In my application, I extend the application class to maintain information relevant to the authenticated user. However, after running through some tests where I disallow background process, open up another application(thus killing mine), and reopen mine, I notice that this state is lost(variables returning to their default value). I realized that the Application class doesn’t have any lifecycle callbacks, besides onCreate, like the Activities do, which I thought the elegant solution would be. So I decided to write this information to a SharedPreference, so that it can be recovered when needed. Is this the correct solution, or is there a more elegant one?

Relevant sample code for reference:

public class ApplicationUser extends Application {

    private static final String PREF_KEY_USER = "prefKeyUser";
    private static final String PREF_KEY_ID = "prefKeyID";
    private static final String PREF_KEY_FIRST_NAME = "prefKeyFirstName";
    private static final String PREF_KEY_LAST_NAME = "prefKeyLastName";
    private static final String PREF_KEY_EMAIL = "prefKeyEmail";

    private User mUser;
    private SharedPreferences mSharedPreferences;


    @Override
    public void onCreate() {
    super.onCreate();
        mSharedPreferences = getSharedPreferences(PREF_KEY_USER,MODE_PRIVATE);
    }

    /**
     * 
     * @return the user
     */
    public User getUser() {
        if (mUser == null) {
            int id = mSharedPreferences.getInt(PREF_KEY_ID, -1);
            String firstName = mSharedPreferences.getString(PREF_KEY_FIRST_NAME, "");
            String lastName = mSharedPreferences.getString(PREF_KEY_LAST_NAME, "");
            String email = mSharedPreferences.getString(PREF_KEY_EMAIL, "");
            mUser = new User(id, firstName, lastName, email);
        }
        return mUser;
    }

    /**
     * sets user, writing to shared preferences as well
     * @param user
     */
    public void setUser(User user) {
        mUser = user;
        Editor e = mSharedPreferences.edit();
        e.putInt(PREF_KEY_ID, user.getID());
        e.putString(PREF_KEY_FIRST_NAME, user.getFirstName());
        e.putString(PREF_KEY_LAST_NAME, user.getLastName());
        e.putString(PREF_KEY_EMAIL, user.getEmail());
        e.commit();
    }
}
  • 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-17T15:13:23+00:00Added an answer on June 17, 2026 at 3:13 pm

    It seems like the way I did it was the correct way, so I am just going to add this as the answer. If this is the incorrect procedure, please correct me.

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

Sidebar

Related Questions

So I am trying extend the base Application class and add member variables to
I have an application where I extend Application class and override the onCreate method.
I've have quite small asp.net MVC 2 application to maintain/extend. Currently it uses hand
I'm writing an Android application that allows a user to maintain a list of
In my web application I have maintain table for the user records. Records are
I am trying to write a mapView application .When I extend my class with
I have the following situation. There is some very common class in my application
I have numerous controllers in my application that extend a central controller class. Currently,
two classes extend Application One class I've registered in the Manifest and am using
My application extends the ui.Manager class. Is it possible to enter the app without

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.