I got stuck while debugging a situation which involved orientation change and some null returns.
My question is divided into :
1) What callbacks of Activity's lifecycle are executed when orientation is changed.
2) What callbacks of Fragment's lifecycle are executed when orientation is changed.
3) If we merge point 2 and 3 (which is usually the case with fragments overlying an Activity), what will be the callback execution flow in terms of an Activity having a Fragment(flow of execution?).
EDIT
Shedding some light on the question, if no configChanges are defined in android manifest file i.e. :
If any configuration change occurs that is not selected to be reported by that attribute, then instead of reporting it the system will stop and restart the activity (to have it launched with the new configuration).
What Lifecycle callbacks of activity and fragment will be executed?
First onPause(), onStop() and onDestroy() will be called simultaneously and after some time when the activity restarts the onCreate(), onStart() and onReume() methods will be called simultaneously. So when you change the orientation of a screen the Activity will be restarted.
When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.
In this method, when you switch from Portrait to Landscape, a method is called, onConfigurationChanged method.
For any activity, you will have to save whatever state you need to save in the onResume() method, which is fired every time the activity changes an orientation.
First of all, in AndroidManifest add it to your activity: android:configChanges="orientation"
1). onConfigurationChanged
2). Nothing, but you can implement your method and call it from activity's onConfigurationChanged
3). The flow will be Activity.onConfigurationChanged -> Fragment.yourMethod
Hope this helps.
UPDATED
Here is your flow when your start activity first time:
Activity.onCreate
Activity.onStart
Fragment.onAttach
Fragment.onCreate
Fragment.onCreateView
Fragment.onViewStateRestored
Fragment.onStart
Activity.onResume
Fragment.onResume
Here is flow after orientation changed:
Activity.onPause
Fragment.onPause
Activity.onSaveInstanceState
Fragment.onSaveInstanceState
Activity.onStop
Fragment.onStop
Activity.onDestroy
Fragment.onDestroy
Fragment.onDetach
Fragment.onAttach
Fragment.onCreate
Activity.onCreate
Activity.onStart
Fragment.onCreateView
Fragment.onViewStateRestored
Fragment.onStart
Activity.onRestoreInstanceState
Activity.onResume
Fragment.onResume
Here is code to check it:
public class FooActivity extends FragmentActivity {
private static final String LOG_TAG = FooActivity.class.getSimpleName() + "_TAG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(LOG_TAG, "Activity.onCreate");
setContentView(R.layout.activity_foo);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new BarFragment())
.commit();
}
}
@Override
protected void onPause() {
Log.i(LOG_TAG, "Activity.onPause");
super.onPause();
}
@Override
protected void onStop() {
Log.i(LOG_TAG, "Activity.onStop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.i(LOG_TAG, "Activity.onDestroy");
super.onDestroy();
}
@Override
protected void onResume() {
Log.i(LOG_TAG, "Activity.onResume");
super.onResume();
}
@Override
protected void onStart() {
Log.i(LOG_TAG, "Activity.onStart");
super.onStart();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.i(LOG_TAG, "Activity.onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.i(LOG_TAG, "Activity.onSaveInstanceState");
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.i(LOG_TAG, "Activity.onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class BarFragment extends Fragment {
public BarFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(LOG_TAG, "Fragment.onCreateView");
View rootView = inflater.inflate(R.layout.fragment_bar, container, false);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(LOG_TAG, "Fragment.onCreate");
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
Log.i(LOG_TAG, "Fragment.onAttach");
super.onAttach(activity);
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
Log.i(LOG_TAG, "Fragment.onViewStateRestored");
super.onViewStateRestored(savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
Log.i(LOG_TAG, "Fragment.onSaveInstanceState");
super.onSaveInstanceState(outState);
}
@Override
public void onResume() {
Log.i(LOG_TAG, "Fragment.onResume");
super.onResume();
}
@Override
public void onStart() {
Log.i(LOG_TAG, "Fragment.onStart");
super.onStart();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.i(LOG_TAG, "Fragment.onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}
@Override
public void onPause() {
Log.i(LOG_TAG, "Fragment.onPause");
super.onPause();
}
@Override
public void onStop() {
Log.i(LOG_TAG, "Fragment.onStop");
super.onStop();
}
@Override
public void onDetach() {
Log.i(LOG_TAG, "Fragment.onDetach");
super.onDetach();
}
@Override
public void onDestroy() {
Log.i(LOG_TAG, "Fragment.onDestroy");
super.onDestroy();
}
}
}
onConfigurationChanged() method
@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();
} }
add this code in your AndroidManifest.xml
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
You can also check these Activity and Fragment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With