Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The key for save / restore is a unique id.</p> <p>Only views with id set (either on xml files or dynamically created) can hold their state on configuration changes, such as device rotation.</p> <p>File CustomTextView.java</p> <pre><code>package example; import android.content.Context; import android.os.Bundle; import android.os.Parcelable; import android.util.AttributeSet; import android.widget.TextView; public class CustomTextView extends TextView { private static final String INSTANCE_STATE = "instance_state"; private static final String VALUE = "value"; private int value; public CustomTextView(Context context) { this(context, null); } public CustomTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // init ... setId(R.id.custom_view); value = 123; } // methods ... @Override public Parcelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState()); bundle.putInt(VALUE, value); // other fields... return bundle; } @Override public void onRestoreInstanceState(Parcelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; value = bundle.getInt(VALUE); // other fields... state = bundle.getParcelable(INSTANCE_STATE); } super.onRestoreInstanceState(state); } } </code></pre> <p>An easy way to get a unique id is statically.</p> <p>File ids.xml (in res/values)</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;item type="id" name="custom_view" /&gt; &lt;/resources&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload