1. Download our Official Android App: Forums for Android!

Apps Android Fragment onDestroy called twice during a orientation change

Discussion in 'Android Development' started by basecode78, Jan 19, 2014.

  1. basecode78

    basecode78 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Jan 19, 2014

    Jan 19, 2014
    2
    0
    5
    I don't understand why onDestroy is being logged twice for the fragment class in the the following code when the orientation of the device changes. Its logged once for the activity class, but twice for the fragment. Can someone explain to me what I am doing wrong, or why this is happening?

    Code (Text):
    1. public class ExampleActivity extends Activity {
    2.  
    3.     protected String LOG_TAG = ExampleActivity.class.getSimpleName();
    4.  
    5.     private FrameLayout mFragmentHolder;
    6.  
    7.     @Override
    8.     protected void onCreate(Bundle savedInstanceState) {
    9.         super.onCreate(savedInstanceState);
    10.  
    11.         LogUtil.i(LOG_TAG, "onCreate");
    12.  
    13.         setContentView(R.layout.activity_main);
    14.  
    15.         mFragmentHolder = (FrameLayout) findViewById(R.id.root);
    16.  
    17.         FragmentManager fm = getFragmentManager();
    18.         FragmentTransaction ft = fm.beginTransaction();
    19.         ft.replace(mFragmentHolder.getId(),MyFragment.newInstance());
    20.         ft.commit();
    21.     }
    22.  
    23.     @Override
    24.     protected void onDestroy() {
    25.         super.onDestroy();
    26.         LogUtil.i(LOG_TAG, "onDestroy");
    27.     }
    28.  
    29.     @Override
    30.     protected void onPause() {
    31.         super.onPause();
    32.         LogUtil.i(LOG_TAG, "onPause");
    33.     }
    34.  
    35.     @Override
    36.     protected void onRestoreInstanceState(Bundle savedInstanceState) {
    37.         super.onRestoreInstanceState(savedInstanceState);
    38.         LogUtil.i(LOG_TAG, "onRestoreInstanceState");
    39.     }
    40.  
    41.     @Override
    42.     protected void onResume() {
    43.         super.onResume();
    44.         LogUtil.i(LOG_TAG, "onResume");
    45.     }
    46.  
    47.     @Override
    48.     protected void onStart() {
    49.         super.onStart();
    50.         LogUtil.i(LOG_TAG, "onStart");
    51.     }
    52.  
    53.     @Override
    54.     protected void onStop() {
    55.         super.onStop();
    56.         LogUtil.i(LOG_TAG, "onStop");
    57.     }
    58.  
    59.  
    60. }
    And here is the fragment class

    Code (Text):
    1. public class MyFragment extends Fragment {
    2.  
    3.     protected String LOG_TAG = MyFragment.class.getSimpleName();
    4.  
    5.     public static Fragment newInstance(){
    6.         return new MyFragment();
    7.     }
    8.  
    9.     @Override
    10.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    11.             Bundle savedInstanceState) {
    12.  
    13.         View view = inflater.inflate(R.layout.screen_login, container,
    14.                 false);
    15.  
    16.         return view;
    17.     }
    18.  
    19.     @Override
    20.     public void onDestroy() {
    21.         super.onDestroy();
    22.         LogUtil.i(LOG_TAG, "onDestroy");
    23.     }
    24.  
    25.     @Override
    26.     public void onPause() {
    27.         super.onPause();
    28.         LogUtil.i(LOG_TAG, "onPause");
    29.     }
    30.  
    31.     @Override
    32.     public void onResume() {
    33.         super.onResume();
    34.         LogUtil.i(LOG_TAG, "onResume");
    35.     }
    36.  
    37.     @Override
    38.     public void onStart() {
    39.         super.onStart();
    40.         LogUtil.i(LOG_TAG, "onStart");
    41.     }
    42.  
    43.     @Override
    44.     public void onStop() {
    45.         super.onStop();
    46.         LogUtil.i(LOG_TAG, "onStop");
    47.     }
    48. }
    And here is the output from logcat

    Code (Text):
    1. 01-17 22:04:34.661: I/BaseApplication(21513): [0.0.7]-[BaseApplication]-[main]-[01/17/2014 22:04:34] onConfigurationChanged
    2. 01-17 22:04:34.706: I/MyFragment(21513): [0.0.7]-[MyFragment]-[main]-[01/17/2014 22:04:34] onPause
    3. 01-17 22:04:34.711: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onPause
    4. 01-17 22:04:34.721: I/MyFragment(21513): [0.0.7]-[MyFragment]-[main]-[01/17/2014 22:04:34] onStop
    5. 01-17 22:04:34.726: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onStop
    6. 01-17 22:04:34.731: I/MyFragment(21513): [0.0.7]-[MyFragment]-[main]-[01/17/2014 22:04:34] onDestroy
    7. 01-17 22:04:34.736: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onDestroy
    8. 01-17 22:04:34.766: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onCreate
    9. 01-17 22:04:34.866: I/MyFragment(21513): [0.0.7]-[MyFragment]-[main]-[01/17/2014 22:04:34] onDestroy
    10. 01-17 22:04:34.876: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onStart
    11. 01-17 22:04:34.881: I/MyFragment(21513): [0.0.7]-[MyFragment]-[main]-[01/17/2014 22:04:34] onStart
    12. 01-17 22:04:34.886: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onRestoreInstanceState
    13. 01-17 22:04:34.891: I/ExampleActivity(21513): [0.0.7]-[ExampleActivity]-[main]-[01/17/2014 22:04:34] onResume
    14. 01-17 22:04:34.896: I/MyFragment(21513): [0.0.7]-[MyFragment]-[main]-[01/17/2014 22:04:34] onResume
     

    Advertisement

  2. basecode78

    basecode78 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Jan 19, 2014

    Jan 19, 2014
    2
    0
    5
    Just bumping this up.. would really like to figure this one out..
     

Share This Page

Loading...