3

Well, I assumed that it should be as simple as

fragmentManager.beginTransaction().replace(R.id.container, otherFragment).commit();

Should do that, but indeed not.

Even fragmentManager.beginTransaction().remove(fragment); doesn't kill it.

Other fragment loads, but in the Fragment I'm trying to kill there is a SensorEventListener which continue to react even after the fragment was replaced. I can hear the sounds and stuff.

Of course, I can make a boolean and switch it, but it seems to me there should be another way to simply destroy this object.

This is how I init fragment :

  public static BaseGameFragment newInstance(int mode) {
      BaseGameFragment.mode = mode;
      BaseGameFragment fragment = new BaseGameFragment();   
        return fragment;
  }

I also try to call onDestroy(); but it doesn't stop sensor's reaction and, therefore, all other processes within fragment.

Vlad Alexeev
  • 2,072
  • 6
  • 29
  • 59
  • Please show [MCVE] especially with the *SensorEventListener*. – t0mm13b May 22 '16 at 15:25
  • Not sure if you can do it, try to call a method in `Activity` that does cleanup in `onDetach` of the `Fragment`. – jaibatrik May 22 '16 at 15:28
  • @jaibatrik but how can I do that cleanup? I mean, I can manually make sensorListener = null and others but still I will have fragment in the memory. I assumed it is destroyed upon replacement. But it seems it doesn't – Vlad Alexeev May 22 '16 at 15:50
  • @user2976267 try this answer.... http://stackoverflow.com/a/6198068/3678308 – Muhammad Waleed May 22 '16 at 16:16
  • are you unregistering the sensor event listener prior to removing the fragment? – t0mm13b May 22 '16 at 16:33
  • @exception-lover Thanks, but still even if I exit Activity, the sounds from listener are still there. So I decided to just put a boolean. Which is not a good desicion – Vlad Alexeev May 22 '16 at 16:33

2 Answers2

0

Try removing the Fragment from the transaction using a Tag. Something like this:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MyFragment fragment = new MyFragment();
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.mainLayout, fragment, "FragmentTransaction_1");
    transaction.commit();
}

public void getRidOfFragment(View v){
    MyFragment fragment = new MyFragment();
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.remove(manager.findFragmentByTag("FragmentTransaction_1"));
    transaction.commit();
}
Jorge Torres
  • 1,806
  • 1
  • 11
  • 6
  • 2
    Thanks, but I specified that I tried remove() and sensor within the removed fragment is keeping on reacting and still plays sounds etc. Fragment is detached from activity but obviously not destroyed – Vlad Alexeev May 22 '16 at 16:01
0

It is a old question, but for the sake of completeness, the right way is to create a method within the fragment to stop the sensor; Invoke this method before removing the fragment.