At least I believe that is the problem. From the API documentation:
inflate(int resource, ViewGroup root) Inflate a new view hierarchy from the specified xml resource.
I've been fighting with an orientation change bug for some time now, and I think I have isolated the origin of the problem. The following code does not behave as I would expect:
SeekBarTestActivity.java
public class SeekBarTestActivity extends Activity {
private LinearLayout layout;
private InflateSeekBar bar1;
private InflateSeekBar bar2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
bar1 = new InflateSeekBar(this, layout);
bar2 = new InflateSeekBar(this, layout);
}
}
InflateSeekBar.java
public class InflateSeekBar {
private SeekBar seekBar;
public InflateSeekBar(Context ctx, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(ctx);
View view = inflater.inflate(R.layout.seekbar_layout, null);
seekBar = (SeekBar) view.findViewById(R.id.seekBar);
seekBar.setMax(300);
parent.addView(view);
}
}
The .xml resource file is just as one would expect, a SeekBar inside a LinearLayout. What I am expecting is two objects each containing a new SeekBar, not tied to one another in any way. Furthermore, I would not expect the SeekBars to retain their progress on an orientation change, instead they should be destroyed and recreated with the activity. However, not only does the second SeekBar retain it's progress, but the first SeekBars progress matches the second. I'm hoping that someone could explain to me why the seekbars are behaving this way so that I can use this knowledge to fix the bugs in my program. Thank you for any knowledge in advance.
Edit - Solved: Just to be clear, my problem was the fact that I was not giving unique ID's to the SeekBars after they were inflated, causing the JVM to treat them as the same object in some respects. Thank you for your help Emanuel Moecklin, that bug was driving me nuts!