在片段上还原ViewResume的onResume

时间:2018-11-01 04:06:49

标签: android android-fragments

我有9个片段,可以在活动中使用选项卡导航进行切换。在这些片段的每个onPause方法中,我都将所有EditText,Spinner和CheckBox值保存到数据库中。然后,在这些片段的每个onResume方法上,我从数据库中检索这些值,并将其值填充到EditTexts,Spinners和CheckBoxes中。我已经检查了onPause和onResume方法中的值,并且它们已正确存储和检索。我不确定为什么UI状态没有正确实例化。 EditTexts和Spinners可以正确加载,但是当我在片段之间切换时,有时会取消选中复选框。因为此方法可用于我的所有其他活动,我是否缺少某些特定于片段的内容?据我所知,onResume方法是片段再次打开时要调用的最后一个方法。

在onPause中,我从数据库中检索对象并调用saveValuesToDB函数。

@Override
public void onPause() {
    super.onPause();

    // Get the structure from the DB corresponding to the values in the Shared Preferences
    Context context = getActivity();
    SharedPreferences sharedPref = context.getSharedPreferences(getString(R.string.shared_preferences_filename), Context.MODE_PRIVATE);

    final String project_name = sharedPref.getString("project_name","");
    final String structure_topo_number = sharedPref.getString("structure_topo_number","");

    returned_structure = null;

    try {
        Thread getStructure = new Thread(new Runnable() {
            @Override
            public void run() {
                returned_structure = structureDB.structureDAO().getStructure(structure_topo_number,project_name);
            }
        });

        getStructure.start();
        getStructure.join();
    } catch(Exception e){
        returned_structure = null;
    }

    // Structure already exists in DB. Set its values equal to the current inputted values
    if(returned_structure != null){
        saveValuesToDB(returned_structure);
    }
}

然后在saveValuesToDB中,设置对象的值。

/* Save all currently entered values to the Database */
//region
public void saveValuesToDB(Structure structure){

    structure_being_saved = structure;

    // Measure Down Type
    structure_being_saved.pipe_measure_down_types.set(5,pipe6_measure_down_type_spinner.getSelectedItem().toString());

    // Measure Down
    structure_being_saved.pipe_measure_downs.set(5,pipe6_measure_down_edit_text.getText().toString());

    // Measure Down Angle
    structure_being_saved.pipe_measure_down_angles.set(5,pipe6_measure_down_angle_edit_text.getText().toString());

    // Pipe Material
    structure_being_saved.pipe_materials.set(5,pipe6_material_spinner.getSelectedItem().toString());
}

然后在onResume中,再次加载对象并调用setValuesFromDB函数

@Override
public void onResume() {
    super.onResume();

    // Get the structure from the DB corresponding to the values in the Shared Preferences
    Context context = getActivity();
    SharedPreferences sharedPref = context.getSharedPreferences(getString(R.string.shared_preferences_filename), Context.MODE_PRIVATE);

    final String project_name = sharedPref.getString("project_name","");
    final String structure_topo_number = sharedPref.getString("structure_topo_number","");

    returned_structure = null;

    try {
        Thread getStructure = new Thread(new Runnable() {
            @Override
            public void run() {
                returned_structure = structureDB.structureDAO().getStructure(structure_topo_number,project_name);
            }
        });

        getStructure.start();
        getStructure.join();
    } catch(Exception e){
        returned_structure = null;
    }

    if(returned_structure != null){
        setValuesFromDB(returned_structure);
    }

    latitude = 0.0;
    longitude = 0.0;
}
//endregion

private void setValuesFromDB(Structure structure) {

    current_structure = structure;

    /* For all of the spinners, check if the position of the item from the DB is not null */
    // Measure Down Type
    if (pipe6_measure_down_type_spinner_adapter.getPosition(current_structure.pipe_measure_down_types.get(5)) != -1) {
        pipe6_measure_down_type_spinner.setSelection(pipe6_measure_down_type_spinner_adapter.getPosition(current_structure.pipe_measure_down_types.get(5)));
    } else {
        pipe6_measure_down_type_spinner.setSelection(0);
    }

    // Measure Down
    pipe6_measure_down_edit_text.setText(current_structure.pipe_measure_downs.get(5));
    pipe6_measure_down_edit_text.setSelection(pipe6_measure_down_edit_text.getText().length());

    // Measure Down Angle
    // If the value from the DB is blank, default it to 90
    if (current_structure.pipe_measure_down_angles.get(5).equals("")) {
        pipe6_measure_down_angle_edit_text.setText("90");
    } else {
        pipe6_measure_down_angle_edit_text.setText(current_structure.pipe_measure_down_angles.get(5));
    }
    pipe6_measure_down_angle_edit_text.setSelection(pipe6_measure_down_angle_edit_text.getText().length());

    // Pipe Material
    if (pipe6_material_spinner_adapter.getPosition(current_structure.pipe_materials.get(5)) != -1) {
        pipe6_material_spinner.setSelection(pipe6_material_spinner_adapter.getPosition(current_structure.pipe_materials.get(5)));
    } else {
        pipe6_material_spinner.setSelection(0);
    }
}

0 个答案:

没有答案