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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:20:30+00:00 2026-06-03T23:20:30+00:00

I’m a newbie in Android development. Let me explain my problem. I’ve have a

  • 0

I’m a newbie in Android development.
Let me explain my problem.
I’ve have a checkbox and a Button in my Main activity. When I Check(that is, enable) my checkbox and Click my Button below it, I’ll be moving to another activity and as I click a button in the second activity, I come back to my main Activity. My problem is when I come back, the Checkbox remains UnChecked or Disabled. What do I do to retain its state even after coming back from the second activity? Please Help.

The CheckBox part of XML layout is:

<CheckBox
    android:id="@+id/ckBxAll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ON" />

Skeleton code for Main activity:

Public class TestActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
LayoutInflater mInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View mMainView = mInflater.inflate(R.layout.main, null);        
setContentView(mMainView);
 Enable_chkbox = (CheckBox)mMainView.findViewById(R.id.ckBxAll);
if(b_onResume==true)
    {
      Enable_chkbox.setChecked(b_onResume);
   }
Enable_chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub




//do something



        });//Enable_chkbox
Compose_btn=(Button)findViewById(R.id.btnCompose);
        Compose_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent gotoComposeMessage=new Intent(TestActivity.this,ComposeMessage.class);
                startActivity(gotoComposeMessage);
            }
        });
}
@Override
    protected void onResume() {
      super.onResume();
     b_onResume=  Enable_chkbox.isChecked();

    }
   @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unregisterReceiver(myreceiver);
        this.finish();
      }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
       super.onSaveInstanceState(savedInstanceState);
       savedInstanceState.putBoolean("Enable_chkbox",true);       

    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
        boolean myBoolean = savedInstanceState.getBoolean("Enable_chkbox");
    }  

}

My Second Activity:

public class ComposeMessage extends Activity{
    Button SaveSMS_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.composemessage);
        SaveSMS_btn=(Button)findViewById(R.id.btnSave);
        SaveSMS_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent gotoHome=new Intent(getApplicationContext(),BlockTestActivity.class);
                Bundle BundleToCarryMessage = new Bundle();
                EditText EnterSMS_edt=(EditText)findViewById(R.id.edtEnterSMS);
                BundleToCarryMessage.putString("Message", EnterSMS_edt.getText().toString());
                gotoHome.putExtras(BundleToCarryMessage);
                startActivity(gotoHome);
            }
        });

    }

}

The XML file used in the second activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/edtEnterSMS"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>
  • 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-03T23:20:32+00:00Added an answer on June 3, 2026 at 11:20 pm

    There is another way.
    You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter like this:

    The next code is for saving the check box state, before moving to the second activity:

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    
      savedInstanceState.putBoolean("MyCheckBox", enable);
    
      super.onSaveInstanceState(savedInstanceState);
    }
    

    And this is when you restore the check box state:

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
    
      boolean myBoolean = savedInstanceState.getBoolean("MyCheckBox");
    
    }
    

    EDIT:

    public class TestActivity extends Activity implements OnCheckedChangeListener, OnClickListener  {
    
            private CheckBox Enable_chkbox;
            private View mMainView;
            private Button Compose_btn;
            private boolean myBoolean = false;;
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                LayoutInflater mInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
                mMainView = mInflater.inflate(R.layout.main, null);        
                setContentView(mMainView);
                initView();
            }
    
            private void initView(){
    
                 Enable_chkbox = (CheckBox)mMainView.findViewById(R.id.ckBxAll);
                 Enable_chkbox.setOnCheckedChangeListener(this);
                 Compose_btn=(Button)mMainView.findViewById(R.id.btnCompose);
                 Compose_btn.setOnClickListener(this);
            }
    
            @Override
            protected void onResume() {
              super.onResume();
              Enable_chkbox.setChecked(myBoolean);
    
            }
    
            @Override
            public void onSaveInstanceState(Bundle savedInstanceState) {
               super.onSaveInstanceState(savedInstanceState);
               savedInstanceState.putBoolean("Enable_chkbox", Enable_chkbox.isChecked());      
            }
    
            @Override
            protected void onRestoreInstanceState(Bundle savedInstanceState) {
                super.onRestoreInstanceState(savedInstanceState);
                myBoolean = savedInstanceState.getBoolean("Enable_chkbox");
            }
    
            @Override
            public void onClick(View arg0) {
                //do something
            }
    
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //do something
            }  
    }
    

    ‘

    Hope it helps.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.