Reputation: 11829
I am inspecting the state of a checkbox. I am using the code below. When app starts (checkbox is unchecked), toast message says "unchecked". But when i open another screen and then go back, it doesn't say anymore. How to do that? Partial code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cb1 = (CheckBox) findViewById(R.id.CheckBox01);
if (cb1.isChecked())
{
Toast.makeText(main.this, "checked", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(main.this, "NOT checked", Toast.LENGTH_SHORT).show();
}
}
In this code i also have a
cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
//blablabla
}
});
part where i am inspecting the state of the checkbox when user clicks it.
I want to inspect the state of checkbox every time this screen shows.
Upvotes: 1
Views: 2393
Reputation: 7486
use onResume()
instead of onCreate()
.
here you can see the activity's lifecycle diagram, onCreate()
is only called once, at the beginning of the activity's life...onResume()
on the other hand is call each time the activity comes back to front.
However, the OnCheckedChangeListener
stuff should be declared in onCreate()
or onStart()
as it will persist.
Upvotes: 3