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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:47:41+00:00 2026-06-15T23:47:41+00:00

I want to check a string in SharedPreferences used it to store username and

  • 0

I want to check a string in SharedPreferences used it to store username and password, so if username and password is not null or empty it will be directed to home, otherwise if the username and password is empty it will be directed to login page.

This is my code to check the SharedPreferences string, but it is not working..

if(PreferenceConnector.USERNAME!=null){
    Intent intent = new Intent(MainActivity.this,MainHome_Activity.class);
    startActivity(intent);
} else {
   Intent intent = new Intent(MainActivity.this,LoginFormPegawai_Activity.class);
   startActivity(intent);
}

I tried to check it through toast with this code, and after I tried this, I get SharedPreferences string is not null or empty..

btn_logout_pegawai.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {

      //remove the SharedPreferences string
      PreferenceConnector.getEditor(this).remove(PreferenceConnector.USERNAME)
            .commit();
      PreferenceConnector.getEditor(this).remove(PreferenceConnector.PASSWORD)
            .commit();  

//checking the SharedPreferences  string
         if(PreferenceConnector.USERNAME!=null){
            Toast.makeText(MainHome_Activity.this,"not null", Toast.LENGTH_SHORT).show();       
         }
         else {
           Toast.makeText(MainHome_Activity.this,"null", Toast.LENGTH_SHORT).show();
        }
    }
 });

How can I correctly check the SharedPreferences string whether its empty or null?

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-15T23:47:42+00:00Added an answer on June 15, 2026 at 11:47 pm

    Do this:

    SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
    String username = myPrefs.getString("USERNAME",null);
    String password = myPrefs.getString("PASSWORD",null);
    

    if username and password are present, they will have a value otherwise they will be null.

    if (username != null && password != null )
    {
        //username and password are present, do your stuff
    }
    

    You don’t have to check their presence separately. Trying to retrieve their value will return null ( if not present ) or the value ( if present ) automatically.

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

Sidebar

Related Questions

I want to check whether string is empty or not when i create object=Shared.getLocal(abc);
i want to check if a string only contains correct letters. I used Char.IsLetter
I want to check if a string is ending with .php extension, if not
I want to check if a file contains a specific string or not in
I want to check that Java String or character array is not just made
I want to check whether a string is palindrome or not using Linq. Updated:
I want to check if a string object of 20 characters has only null
I want to check if a string is a palindrome or not. I would
Like if I want to check whether a string is a palindrome or not,
I want to check a string to allow only a-z A-Z 0-9 . /

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.