How to handle ** Grid View ** when changing a configuration?

I use grid view in my application activity. Each row of the grid view contains three flags that can be set to selected / not selected based on what the user wants to query from the database. The activity also includes editText, which causes the onScreenKeyboard to appear every time the activity starts, and this is where the problem arises. OnScreenKeyboard, when an alarm grid appears, and some of its flags simply disappear. My idea was to update the grid every time a configuration change. I tried to handle this by returning an object via onRetainNonConfigurationInstance () . The object contains a list of arrays to populate my gridview lines, but onCreate () , when I use getLastNonConfigurationInstance () to retrieve the returned object, it shows null. Can someone please suggest me how to deal with this problem or if there is any other access with which I can make my gridView behave normally when changing the configuration. The code is below, and I want it to become clear. I added a keyboardHidden configuration change in the manifest file, but when the keyboard appears, onConfigurationChanged () sometimes fails

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.failureselection); findLocalWidgets(); //Initializes Layout Objects if(configchanged){ //Re populate grid view customDataGridRows = (ArrayList<CustomGridViewRow>) getLastNonConfigurationInstance(); dgvwFailures.setAdapter(new CustomGridViewAdapter(this, customDataGridRows)); configchanged = false; }else{ fillFailuresList(customDataGridRows); dgvwFailures.setAdapter(new CustomGridViewAdapter(this, customDataGridRows)); } } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); setContentView(R.layout.failureselection); configchanged = true; } @Override public Object onRetainNonConfigurationInstance() { return customDataGridRows; } private boolean fillFailuresList( ArrayList<CustomGridViewRow> customDataGridRows) { boolean isFilled = false; try { // Adding Failures data customDataGridRows.add(new CustomGridViewRow(false, "Hood", false, "Assembly Defect", false, "Masking Poor")); customDataGridRows.add(new CustomGridViewRow(false, "Floor", false, "Forget Work", false, "Assembly Defect")); customDataGridRows.add(new CustomGridViewRow(false, "Grill", false, "Incorrect Assembly", false, "Bad Company")); customDataGridRows.add(new CustomGridViewRow(false, "R Right Frame", false, "Interference", false, "Fix Large Effort")); customDataGridRows.add(new CustomGridViewRow(false, "R Left Frame", false, "Leakage", false, "High Incidence")); customDataGridRows.add(new CustomGridViewRow(false, "R Frame", false, "Dirt", false, "Recurrence")); customDataGridRows.add(new CustomGridViewRow(false, "Outside R Frame", false, "Decal", false, "Checking")); customDataGridRows.add(new CustomGridViewRow(false, "F Right Frame", false, "Other", false, "Foreign Body")); customDataGridRows.add(new CustomGridViewRow(false, "F Left Frame", false, "", false, "Not Caulking")); customDataGridRows.add(new CustomGridViewRow(false, "F Frame", false, "", false, "Painting Defect")); customDataGridRows.add(new CustomGridViewRow(false, "Outsie F Frame", false, "", false, "Other")); customDataGridRows.add(new CustomGridViewRow(false, "", false, "", false, "")); // Populating Failures grid view // dgvwFailures.setAdapter(new CustomGridViewAdapter(this, // customDataGridRows)); isFilled = true; } catch (Exception e) { e.getMessage(); } return isFilled; } 
+6
source share
3 answers

Parameter pair:

Hint: Remove all <requestFocus /> from the EditText layout. Although Android will set focus on the first panoramic view.

Another hack: How to remove focus without setting focus on another control?

+4
source

If your onConfigurationChanged is not always called, consider adding

 android:configChanges="keyboardHidden|orientation" 

in your manifest. This will also trigger an onConfigurationChanged call when the device orientation changes. Also, I'm not sure with setContentView inside your onCOnfigurationChanged. Good luck.

+2
source

Instead, you can write onSaveInstanceState(Bundle bundle) and save the current state of all user interface components in this bundle.putBoolean("check_box_1", true) for example, bundle.putBoolean("check_box_1", true) ). When changing the configuration of Android, the system will call this method.

After a configuration change, when the system recreates your activity by calling onCreate(Bundle saveInstanceState) , the same instance of Buldle will be provided, so this time saveInstanceState will not be null . From this Bundle extract previously saved values ​​(for example, saveInstanceState.getBoolean("check_box_1") ) and set them in the appropriate fields.

This is a Santander way to handle this, and because of this, onCreate() has a Bundle input parameter.

+2
source

Source: https://habr.com/ru/post/904061/


All Articles