1

I am working on an app that uses 5 tabs and each tab has a different fragment. In all of these fragments I have from 1 up to 10 dynamically created tablelayouts and each one of them can have as many rows as the user wants. Each row may have up to 16 columns of EditText, Spinner, Checkbox. My problem is that when I rotate my phone I lose all the data inside these tablelayouts. My ViewPagerAdapter that hosts these fragments extends FragmentStatePagerAdapter. I am not losing any data outside the tables. I understand that when I rotate the phone they are recreated but I want to keep my data. I do not want to create a database because when I finish adding data in my app I want to save everything to a .txt file. This is code for one of my tables

 table18 = (TableLayout) view.findViewById(R.id.blg_parameter18_table);
    TableRow.LayoutParams tlparams18 = new TableRow.LayoutParams(
            TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
    //Create header
    TableRow table18HeaderRow = new TableRow(view.getContext());
    table18HeaderRow.setLayoutParams(tlparams18);
    TextView table18HeaderColumn0 = new TextView(view.getContext());
    setTableHeaderView(table18HeaderRow, table18HeaderColumn0, R.string.blg_parameter18_0, tlparams18);
    TextView table18HeaderColumn1 = new TextView(view.getContext());
    setTableHeaderView(table18HeaderRow, table18HeaderColumn1, R.string.blg_parameter18_1, tlparams18);
    TextView table18HeaderColumn2 = new TextView(view.getContext());
    setTableHeaderView(table18HeaderRow, table18HeaderColumn2, R.string.blg_parameter18_2, tlparams18);
    TextView table18HeaderColumn3 = new TextView(view.getContext());
    setTableHeaderView(table18HeaderRow, table18HeaderColumn3, R.string.blg_parameter18_3, tlparams18);
    TextView table18HeaderColumn4 = new TextView(view.getContext());
    setTableHeaderView(table18HeaderRow, table18HeaderColumn4, R.string.blg_parameter18_4, tlparams18);
    TextView table18HeaderColumn5 = new TextView(view.getContext());
    setTableHeaderView(table18HeaderRow, table18HeaderColumn5, R.string.blg_parameter18_5, tlparams18);
    TextView table18HeaderColumn6 = new TextView(view.getContext());
    setTableHeaderView(table18HeaderRow, table18HeaderColumn6, R.string.blg_parameter18_6, tlparams18);
    TextView table18HeaderColumn7 = new TextView(view.getContext());
    setTableHeaderView(table18HeaderRow, table18HeaderColumn7, R.string.blg_parameter18_7, tlparams18);
    TextView table18HeaderColumn8 = new TextView(view.getContext());
    setTableHeaderView(table18HeaderRow, table18HeaderColumn8, R.string.blg_parameter18_8, tlparams18);
    TextView table18HeaderColumn9 = new TextView(view.getContext());
    setTableHeaderView(table18HeaderRow, table18HeaderColumn9, R.string.blg_parameter18_9, tlparams18);
    table18.addView(table18HeaderRow);
    createRowTable18(table18, table18NumOfRows, tlparams18);


public void createRowTable18(TableLayout table18, int numOfRows, TableRow.LayoutParams tlparams18) {
    TableRow table18Row = new TableRow(table18.getContext());
    table18Row.setLayoutParams(tlparams18);
    table18Row.setId(numOfRows);
    CheckBox table18RowColumn1 = new CheckBox(getContext());
    CheckBox table18RowColumn2 = new CheckBox(getContext());
    CheckBox table18RowColumn3 = new CheckBox(getContext());
    CheckBox table18RowColumn4 = new CheckBox(getContext());
    CheckBox table18RowColumn5 = new CheckBox(getContext());
    CheckBox table18RowColumn6 = new CheckBox(getContext());
    EditText table18RowColumn7 = new EditText(getContext());
    final TextView table18RowColumn8 = new TextView(getContext());
    EditText table18RowColumn9 = new EditText(getContext());
    Spinner spinner18 = new Spinner(getContext());
    ArrayAdapter<CharSequence> adapter18 = ArrayAdapter.createFromResource(getContext(),
            R.array.blg_parameter18_1_array, android.R.layout.simple_spinner_item);
    adapter18.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner18.setAdapter(adapter18);
    table18Row.addView(spinner18);
    spinner18.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String selection = (String) parent.getItemAtPosition(position);
            if (!TextUtils.isEmpty(selection)) {
                if (selection.equals(getString(R.string.blg_parameter_18_1_0))) {
                    table18RowColumn8.setText(null);
                } else if (selection.equals(getString(R.string.blg_parameter_18_1_a))) {
                    table18RowColumn8.setText("kWh");
                } else if (selection.equals(getString(R.string.blg_parameter_18_1_b))) {
                    table18RowColumn8.setText("lt");
                } else if (selection.equals(getString(R.string.blg_parameter_18_1_c))) {
                    table18RowColumn8.setText("lt");
                } else if (selection.equals(getString(R.string.blg_parameter_18_1_d))) {
                    table18RowColumn8.setText("Nm^3");
                } else if (selection.equals(getString(R.string.blg_parameter_18_1_e))) {
                    table18RowColumn8.setText("Nm^3");
                } else if (selection.equals(getString(R.string.blg_parameter_18_1_f))) {
                    table18RowColumn8.setText("kg");
                } else if (selection.equals(getString(R.string.blg_parameter_18_1_g))) {
                    table18RowColumn8.setText("kWh");
                }
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
    setTableRowCheckBox(table18Row, table18RowColumn1);
    setTableRowCheckBox(table18Row, table18RowColumn2);
    setTableRowCheckBox(table18Row, table18RowColumn3);
    setTableRowCheckBox(table18Row, table18RowColumn4);
    setTableRowCheckBox(table18Row, table18RowColumn5);
    setTableRowCheckBox(table18Row, table18RowColumn6);
    table18RowColumn7.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    setTableRowEditView(table18Row, table18RowColumn7, tlparams18);
    setTableRowTextView(table18Row, table18RowColumn8, tlparams18);
    table18RowColumn7.setInputType(InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_NORMAL);
    setTableRowEditView(table18Row, table18RowColumn9, tlparams18);
    table18.addView(table18Row);
}


public void addRow18() {
        createRowTable18(table18, table18NumOfRows, tlparams);
        table18NumOfRows++;
    }

public void removeRow18() {
    if (table18NumOfRows > 0) {
        table18.removeViewAt(table18NumOfRows);
        table18NumOfRows--;
    } else {
        Toast.makeText(getContext(), "Δεν υπάρχουν γραμμές στον πίνακα", Toast.LENGTH_SHORT).show();
    }
}

When I complete inserting data they are too many to save and recall them one by one everytime i rotate the phone. Any ideas?

Ioanna Dln
  • 309
  • 2
  • 13

2 Answers2

0

When your orientation changes, Android destroys your current activity and creates a new activity again, so you must manage your state. You can use onSaveInstanceState to save the state and then get the same it in onCreate, for example:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable("MyObject", myObject);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       

    if (savedInstanceState != null) {
        final MyDataType myData = savedInstanceState.getString("MyObject"));
        // fill your layout...
    }
}

You can manage the state of your fragments too, please check here. Fragments can be "retain his instance" please read this or this.

Here another great post. In some projects where I worked, when the application was not prepared for screen rotation, we had to disable this configuration.

I hope this will be useful for you.

Gaston Flores
  • 2,457
  • 3
  • 23
  • 42
  • I think I must go with the option of no rotation because i am going crazy... When I rotate from landscape to portrait I do not lose any data. When I go from portrait to landscape I lose all the data in my dynamically created tables but everything else like edit texts or spinners outside the tables are saved. – Ioanna Dln Oct 17 '17 at 23:11
0

Android will automatically save and restore your tables and the associated text views, but you need to do a couple of things.

  1. Make sure each view you want to save has an id. Only views will an id will be saved. See generateViewId and setId.

Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.

  1. Even with an id set, Android will not by default save the value of a TextView presumably because TextViews tend to be static. You can force a save/restore of a TextView by using freezesText.

android:freezesText

If set, the text view will include its current complete text inside of its frozen icicle in addition to meta-data such as the current cursor position. By default this is disabled; it can be useful when the contents of a text view is not stored in a persistent place such as a content provider. For EditText it is always enabled, regardless of the value of the attribute.

There may be some odds and ends to tend to, but that should be the bulk of the changes needed.

Community
  • 1
  • 1
Cheticamp
  • 61,413
  • 10
  • 78
  • 131