3

I saw that when I execute

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragment myFragment = new MyFragment();
fragmentTransaction.replace(R.id.parent, myFragment, "MyFragment");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

there is no method called on the old fragment. I tried with:

  • onStop();
  • onPause();
  • onDestroy();
  • onDetach();

No one is called, why?

helloimyourmind
  • 994
  • 4
  • 14
  • 30

2 Answers2

1

Maybe there is a typing error in your post or a big misunderstanding. First there is no fragmentManager.replace(), perhaps you meant FragmentTransaction.replace().

If it is so, try onDetach() and onDestroy() again. Example, make sure you have the @Override attribute:

@Override
public void onDetach() {
...
}
The Original Android
  • 6,147
  • 3
  • 26
  • 31
  • I edited my first post, I tried also onDetach(), but is not fired (I tried with a Log Message). Simply I have a parent, and 2 fragment. First I add MyFragment2, next I replace MyFragment2, with MyFragment. I wanna fire the MyFragment2 event when I use fragmentTransaction.replace(R.id.parent, myFragment, "MyFragment"); – helloimyourmind Jul 26 '15 at 11:24
  • @helloimyourmind, Perhaps you should post the code for MyFragment. This aspect of Android should work easily. You will have to respond quicker than you have. It is hard for me or anyone to remember the issue. – The Original Android Jul 26 '15 at 18:41
  • I have the same issue as the OP – Sir NIkolay Cesar The First Feb 07 '17 at 20:17
0

If you are going to change the checked item in navigation drawer, i think the best way is to use onAttachFragment event in the main activity

Ebrahim Karimi
  • 732
  • 8
  • 24