ketom ketom - 1 year ago 55
Android Question

Android orientation save layout child state

I have view like this:
enter image description here

This is a android module. When I use it on another layout:

<com.example.myapp.MyModuleView
android:id="+id/test" ...


When I get the reference i can call MyModuleView classes methods.

MyModuleView v = (MyModuleView)findViewById(R.id.test)


When I create the MySeekBarLayout:

MySeekBarLayout l = new MySeekBarLayout();
l.setProgress(10)


This layout is:


MySeekBarLayout extends LinearLayout


When i add this view to mymoduleview:

v.getLayout().addView(l.getLayout())


So the layout was added to parent layout.
Its work good.

But when i rotate my phone, than the MyModuleView's
onSaveInstanceState
and
onRestoreInstanceState
runned. But the attached layouts recreated without call
onRestoreInstanceState
. How can I save the attached view's state?
Thanks!

public class MySeekBarView<T> extends TableRow {

private SeekBar seekBar;
private TextView seekBarValue;

public MySeekBarView(Context context, String title) {
super(context);
inflate(getContext(), R.layout.layout_seekbar, this);
}

@Override
protected void onViewCreated() {
seekBar = findViewById(R.id.my_seekbar_input);
seekBarValue = findViewById(R.id.my_seekbar_value);
seekBarValue.setText(String.valueOf(seekBar.getProgress()));
}


}


MyModuleView Attach child:

view
is a MySeekBarView istance!

TableRow tr = new TableRow(getContext());
TextView textView = (TextView) factory.inflate(R.layout.textview_layout, null);
textView.setText("Test:");
tr.addView(textView);
tr.addView(view, new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f));
tr.setGravity(Gravity.CENTER_VERTICAL);
table.addView(tr);

Answer Source

If you extend the LinearLayout, you need to set Id because the android dont call onsave and onrestore. so call setID(uniqeId++)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download