I have a custom button in an activity. It works fine. I hit back, then restart the activity, and the button is gone. I created a stripped project to show what the problem is.
The application starts with this activity (just a button that starts Activity2):
public class TestCustomButtonActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(TestCustomButtonActivity.this, Activity2.class);
startActivity(intent);
}
});
}
}
Activity2 has only one button
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/menu_left_button" />
menu_left_button is the xml selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true">
<item android:state_pressed="true"
android:drawable="@drawable/button_left_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_left_hovered" /> <!-- focused -->
<item android:drawable="@drawable/button_left_normal" /> <!-- default -->
</selector>
And that’s all there is to it. You start the application and hit button1, and you are presented with Activity2 and our custom button. And it works fine. But if you hit the back button (close Activity2) and hit button1 again, our custom button is not there! It has disappeared. Any good reason for that?
Target api level is 4 (if this has something to do with it).
Activity2 code is nearly empty:
public class Activity2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
}
}
After several months as you can see, I found the answer to this, by chance. It is posted as an answer in another stackoverflow question : ImageButton does not display a particular drawable. The actual problem as i now know is that the image “button_left_normal” does not show, since it is the first image alphabetically. If the button had a fixed size and not “wrap_content” as dimensions, i would be able to click it, and see that “button_left_pressed” is shown without any problem. I hope this helps someone out there.