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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:16:31+00:00 2026-06-04T22:16:31+00:00

So I have a SharedPreference I’m calling logged_in. I get the preference and check

  • 0

So I have a SharedPreference I’m calling logged_in. I get the preference and check if it’s 1, if it is it’s supposed to start the new activity, otherwise I want it to display to me what it actually is. Here’s the code:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

        prefs = getSharedPreferences("preferences", MODE_PRIVATE);

        // Check if the user is logged in
        String loggedIn = prefs.getString("logged_in", null);

        if(loggedIn != "1") {
            setContentView(R.layout.main);
            TextView textView = (TextView) findViewById(R.id.login_status);
            textView.setText(prefs.getString("logged_in", null));
        }

        else {
            startActivity(new Intent(this, LoggedInActivity.class));
        }
    }

What’s really weird is that it will load the main view instead of starting the LoggedIn Activity, but then it displays the logged_in preference as being a 1. So according to what I’m seeing, it should be starting the new activity, but it’s not. I’m really confused on this. Any help is greatly appreciated.

  • 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-04T22:16:32+00:00Added an answer on June 4, 2026 at 10:16 pm

    You are comparing references instead of string values. That is, you are saying do these two references point to the same object instead of are the content of this two objects the same. You should do instead:

    if( !loggedIn.equals("1") )
    

    Note however that if loggedIn is null, that code will raise a NullPointerException. So this would be more appropriate:

    if( loggedIn == null || !loggedIn.equals("1") )
    

    That said, in this particular case you would be better off by using an int preference.

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

Sidebar

Related Questions

I want to save images in android using SharedPreference. I have two activity classes,
I want to remotely change few SharedPreference settings on my App. I have been
I have an Activity, which contains a bunch of check boxes and a submit
I'm new at android. I have a little idea over sharedPreference. Some tutorials say
I just want to know, if i have a Default SharedPreference then where should
I have an ArrayList of string in my activity and i want to store
I have one Application which has more then 5 Activity I want to know
I have an app with 2 activities, the preference and the main activity, I
I have an AsyncTask in a activity that i only want to run once
I have made a shared preference activity that store the user settings, now i

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.