Note that there are some explanatory texts on larger screens.

plurals
  1. POPersist views across screen rotation
    primarykey
    data
    text
    <p>New to Android so I'm writing small programs to get familiar with how things work. What has been giving me a headache so far are views created at runtime in combination with screen rotation.</p> <p>After having little luck trying to use parcels, I solved the problem by just recreating the views after a rotation.</p> <p>The program will add the entered text as a TextView to a TableLayout below the EditText.</p> <p>Is there a better way of solving this? I could not find any "out of the box" methods for doing this.</p> <pre><code>public class MWEActivity extends Activity { TableLayout table; EditText txtInput; ArrayList&lt;String&gt; savedEntry = new ArrayList&lt;String&gt;(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); table = (TableLayout)this.findViewById(R.id.table); txtInput = (EditText)this.findViewById(R.id.txtInput); txtInput.setOnEditorActionListener(new OnEditorActionListener() { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER &amp;&amp; event.getAction() == KeyEvent.ACTION_DOWN){ TextView newtext = new TextView(getApplicationContext()); newtext.setText(txtInput.getText()); savedEntry.add(newtext.getText().toString()); table.addView(newtext); txtInput.setText(""); return true; } return false; } }); } @Override protected void onSaveInstanceState(Bundle outState) { outState.putStringArrayList("entry", savedEntry); super.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { if(savedInstanceState.getStringArrayList("entry") == null){ savedEntry = new ArrayList&lt;String&gt;(); savedEntry.add("Return was NULL"); }else{ savedEntry = savedInstanceState.getStringArrayList("entry"); } for(String s : savedEntry){ TextView tv = new TextView(getApplicationContext()); tv.setText(s); table.addView(tv); } super.onRestoreInstanceState(savedInstanceState); } } </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.
 

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