Question

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.

After having little luck trying to use parcels, I solved the problem by just recreating the views after a rotation.

The program will add the entered text as a TextView to a TableLayout below the EditText.

Is there a better way of solving this? I could not find any "out of the box" methods for doing this.

public class MWEActivity extends Activity {
    TableLayout table;
    EditText txtInput;
    ArrayList<String> savedEntry = new ArrayList<String>();

    /** 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 && 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<String>();
            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);
    }
}
Was it helpful?

Solution

Either you have to disable orientation change like in previous answer, or just rely on standard facility. It will pause an then recreate your activity. No extra code is necessary. You can also provide different layouts for different orientations:

http://developer.android.com/guide/practices/screens_support.html

OTHER TIPS

You can stop the reloading of the activity by putting

        android:configChanges="orientation"

in the manifest.xml for the activity and implementing onConfigurationChanged.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

If your layout is the same you should not need to do anything else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top