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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:39:42+00:00 2026-06-16T01:39:42+00:00

Shared preferences inside tabhost not working for button pressed state. I am changing the

  • 0

Shared preferences inside tabhost not working for button pressed state.

I am changing the background of the button on pressed state. But when I reboot(off and on) the phone the shared preferences is not saving the state.

refereed from here

Shared preferences for button pressed state inside tabhost not working on reboot

on debugging, isclick is showing as true in if-else condition. I dont understand the problem.

for the first time when I run, I am getting the button with highlighted background and even on reboot also i am getting the same highlited background.

Any help is always appreciated.

private boolean isclick ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    seatdirnbtn = (Button) findViewById(R.id.seatdirnbtn);
    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);

    isclick = prefs.getBoolean("prefName", false);
    System.out.println("bool? " + isclick);

    if (isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    } else if (!isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4);
    } 
}


@Override
public void onRestart() {
    super.onRestart();

    if (isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4);
    } else if (!isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    }
}

@Override
public void onStop() {
    super.onStop();

    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
    editor = prefs.edit();

    editor.putBoolean("prefName", true);
    editor.commit();
}

private View.OnClickListener listner1 = new View.OnClickListener() {
    public void onClick(View v) {
        if (isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);

            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", true);
            editor.commit();
        } else if (!isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4);
            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", false);
            editor.commit();
        }
        isclick = !isclick;
    }
};

EDIT1:

    private boolean isclick  ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);

    isclick = prefs.getBoolean("prefName", true);

    System.out.println("bool? " + isclick);
    Log.i(TAG, " prefname");

    if (isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    } else if (!isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4);
    } 

         public void onPause() {
    super.onPause();
    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
    // boolean isclick = false;
    editor = prefs.edit();
    // editor.clear();
    editor.putBoolean("prefName", false);
    Log.i(TAG, " prefname");
    editor.commit();
}

@Override
public void onRestart() {
    super.onRestart();
      prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
        isclick = prefs.getBoolean("prefName", !isclick);

}

@Override
public void onStop() {
    super.onStop();
    getApplicationContext().unbindService(this);

      prefs = this.getSharedPreferences(prefName, MODE_PRIVATE); // boolean
      isclick = false; editor = prefs.edit(); // editor.clear();
      editor.putBoolean("prefName", !isclick); 
      editor.commit();

      }

         private View.OnClickListener listner1 = new View.OnClickListener() {

    public void onClick(View v) {


        if (isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);

            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", false);
            editor.commit();
            isclick = false;
        } else if (!isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4);
            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", true);
            editor.commit();
            isclick = true;
        }

    }
};
  • 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-16T01:39:43+00:00Added an answer on June 16, 2026 at 1:39 am

    Try with this Edited Solution :

    public class SharedprefsActivity extends Activity  {
    
        protected static final String TAG = "HvacActivity";
        /** Called when the activity is first created. */
        private Button seatdirnbtn;
        private SharedPreferences prefs;
        private String prefName = "MyPref";
        private SharedPreferences.Editor editor;
        private boolean isclick;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            seatdirnbtn = (Button) findViewById(R.id.seatdirnbtn);      
            seatdirnbtn.setOnClickListener(listner1);       
        }
    
        public void onResume() {
            super.onResume();
            getPrefAndButtonState();        
        }
    
        public void onPause() {
            super.onPause();
            setPrefAndButtonState();
        }
    
        @Override
        public void onRestart() {
            super.onRestart();
            getPrefAndButtonState();
        }
    
        @Override
        public void onStop() {
            super.onStop();
            setPrefAndButtonState();
        }
    
        public void getPrefAndButtonState(){
            prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
            isclick = prefs.getBoolean("prefName", false);
            System.out.println("bool? " + isclick);
            if (isclick) {
                seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
            } else if (!isclick) {
                seatdirnbtn.setBackgroundResource(R.drawable.icon4);
            }
        }
    
        public void setPrefAndButtonState(){
            editor = prefs.edit();
            editor.putBoolean("prefName", isclick);
            editor.commit();        
            getPrefAndButtonState();
        }
    
        private View.OnClickListener listner1 = new View.OnClickListener() {    
            public void onClick(View v) {   
                if (isclick) {
                    isclick = false;
                    setPrefAndButtonState();            
                } else if (!isclick) {
                    isclick = true;
                    setPrefAndButtonState();
                }   
            }
        };
    }
    

    I have test well and it works well for me.

    Hope it helps you.

    Thanks.

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

Sidebar

Related Questions

In my app,i want to use Shared Preferences inside a broadcast receiver...But i cant
How would I use shared preferences to store the state of my checkbox for
I am using shared preferences to save 3 string fields. I am opening shared
Can any one differentiate Fragments and shared preferences on android. Because, Right now i
I want my application to reset shared preferences when the application reinstalling let's say
I created a demo for implementing the shared preferences and it worked for me.
Anytime i change my in-app-settings (with shared preferences) i have to use the back
I am making an activity that will edit the shared preferences just when the
there are lots of questions out there related to shared preferences and the alternatives.
I created a sample application using Android TextView with shared preferences . In my

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.