0

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 onRestoreInstanceStaterunned. But the attached layouts recreated without callonRestoreInstanceState. 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);
ketom
  • 2,334
  • 4
  • 17
  • 25

3 Answers3

0

You can override onConfigurationChanged method and handle changes by this way

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

            //Your Code Here
        }
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //Your Code Here
        }
    }
AbhayBohra
  • 2,047
  • 24
  • 36
0

Add these method to your custom view code.

static class SavedState extends BaseSavedState {
        int progress;

        SavedState(Parcelable superState) {
            super(superState);
        }

        private SavedState(Parcel in) {
            super(in);
            progress = in.readInt();
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeInt(progress);
        }

        public static final Parcelable.Creator<SavedState> CREATOR
                = new Parcelable.Creator<SavedState>() {
            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
    }

    @Override
    public Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        SavedState state = new SavedState(superState);

        //save your view states
        state.progress = mProgress;

        return ss;
    }

    @Override
    public void onRestoreInstanceState(Parcelable state) {
        SavedState ss= (SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());

        //restore states.
        setProgress(ss.progress);
    }
Pratik Popat
  • 2,891
  • 20
  • 31
0

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

ketom
  • 2,334
  • 4
  • 17
  • 25