Skip to content

Instantly share code, notes, and snippets.

@OrenBochman
Last active September 8, 2018 06:28
Show Gist options
  • Save OrenBochman/b90d262e2f832b257fa0e3f280082c26 to your computer and use it in GitHub Desktop.
Save OrenBochman/b90d262e2f832b257fa0e3f280082c26 to your computer and use it in GitHub Desktop.
handling configuration changes in fragments and activities

Configuration Changes

getting the cuttent configuration

in an activity:

Configuration config = ContextThemeWrapper.getResources().getConfiguration();

Saving UI state (simple)

  1. override onSaveInstanceState(Bundle b)
  2. override onRestoreInstanceState(Bundle b)

Note: bundle serilization is done on the main thread. Deserilise large items may block and risks an ANR

Hanling Configuration Change

Only do this if you must as there is lots of resource updating done by the framework for you.

  1. override onConfigurationChanged(Configuration newConfig) and check device orientatino

Refrences

// YourActivity.java
public final static String LIST_STATE_KEY = "recycler_list_state";
Parcelable listState;
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
// Save list state
listState = mLayoutManager.onSaveInstanceState();
state.putParcelable(LIST_STATE_KEY, listState);
}
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
// Retrieve list state and list/item positions
if(state != null)
listState = state.getParcelable(LIST_STATE_KEY);
}
@Override
protected void onResume() {
super.onResume();
if (listState != null) {
mLayoutManager.onRestoreInstanceState(listState);
}
}
/** The standard mechanism for savig state.
*/
public class MainActivity extends Activity {
static final String SOME_VALUE = "int_value";
static final String SOME_OTHER_VALUE = "string_value";
/** handling config change */
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
/** alternatively if not handling config change - save state */
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
// Save custom values into the bundle
savedInstanceState.putInt(SOME_VALUE, someIntValue);
savedInstanceState.putString(SOME_OTHER_VALUE, someStringValue);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
/** and restore it later */
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
someIntValue = savedInstanceState.getInt(SOME_VALUE);
someStringValue = savedInstanceState.getString(SOME_OTHER_VALUE);
}
}
<!-- skips restrart on orientation|keyboardHidden configuration changes instead on-->
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
</activity>
<!-- also one can suppress landscape layout conf change by lockinng screen orientation to portrait -->
<activity
android:name="com.techblogon.screenorientationexample.MainActivity"
android:screenOrientation="portrait"
android:label="@string/app_name" >
</activity>
/** We can also saving and restoring Fragment State */
public class MySimpleFragment extends Fragment {
private int someStateValue;
private final String SOME_VALUE_KEY = "someValueToSave";
// Fires when a configuration change occurs and fragment needs to save state
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(SOME_VALUE_KEY, someStateValue);
super.onSaveInstanceState(outState);
}
// Then we can pull data out of this saved state in onCreateView:
// Inflate the view for the fragment based on layout XML
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_simple_fragment, container, false);
if (savedInstanceState != null) {
someStateValue = savedInstanceState.getInt(SOME_VALUE_KEY);
// Do something with value if needed
}
return view;
}
}
/** alternatively we can store it in a fragment manager and use a tag to fetch it if it already exists */
public class RetainedFragment extends Fragment {
// data object we want to retain
private MyDataObject data;
// this method is only called once for this fragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// retain this fragment when activity is re-initialized
setRetainInstance(true);
}
public void setData(MyDataObject data) {
this.data = data;
}
public MyDataObject getData() {
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment