Last active
February 22, 2017 06:37
-
-
Save kingsleyadio/496c36eca245f35bd6ac to your computer and use it in GitHub Desktop.
Fix for crashes caused when screen is rotated more than once while fragment back stack is not empty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Fragment; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
/** | |
* @author ADIO Kingsley O. | |
* @since 06 Jun, 2015 | |
*/ | |
public class BaseFragment extends Fragment { | |
private Bundle savedViewState; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if (savedInstanceState != null) { | |
savedViewState = savedInstanceState.getBundle("savedViewState"); | |
} | |
} | |
@Override | |
public void onViewStateRestored(Bundle savedInstanceState) { | |
super.onViewStateRestored(savedInstanceState); | |
if (savedViewState != null) { | |
onRestoreViewState(savedViewState); | |
} | |
} | |
protected void onRestoreViewState(@NonNull Bundle savedViewState) { | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
if (savedViewState == null) { | |
savedViewState = new Bundle(); | |
} | |
onSaveViewState(savedViewState); | |
} | |
protected void onSaveViewState(@NonNull Bundle savedViewState) { | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
outState.putBundle("savedViewState", savedViewState); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment