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);
}
}