0

in the application I am developing I add fragments dynamically, and now i want to remove them but I cannot, the application crashes. Thanks a lot for the help in advance. Here is my code

Here are declared the things that i use

    static int alarmsAmount = 0;
public android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
public AlarmFragment[] alarmFragment = new AlarmFragment[7];

And here is how i add the fragments

    alarmFragment[getAlarmsAmount()] = new AlarmFragment();
                        alarmFragment[getAlarmsAmount()].setAlarmNumber(getAlarmsAmount());
                        android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentTransaction.add(R.id.alarmListContainer, alarmFragment[getAlarmsAmount()],Integer.toString(getAlarmsAmount()));
                        fragmentTransaction.commit();
                        setAlarmsAmount(getAlarmsAmount() + 1);

Now the problem itself

public void removeAlarm(int alarmNumber) {
    Log.d("informatione", Integer.toString(alarmNumber));

    getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentByTag(Integer.toString(alarmNumber))).commit();


}

Error log

11-03 01:33:22.634  12942-12942/com.rggmiranda.appE/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Activity has been destroyed
        at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1333)
        at android.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
        at android.app.BackStackRecord.commit(BackStackRecord.java:574)
        at com.rggmiranda.app.Alarms.removeAlarm(Alarms.java:66)
        at com.rggmiranda.app.AlarmSettings$1.onClick(AlarmSettings.java:30)
        at android.view.View.performClick(View.java:4340)
        at android.view.View$PerformClick.run(View.java:17642)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5185)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
        at dalvik.system.NativeStart.main(Native Method)

The log points at getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentByTag(Integer.toString(alarmNumber))).commit();

Rodrigo Miranda
  • 113
  • 3
  • 13

1 Answers1

0

In the top you're using getSupportFragmentManager(), but when removing, you use getFragmentManager(). Make sure you use getSupportFragmentManager() at all places.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
  • Hi, thanks for answering, I am still unable to make it work, the app crashes at that line, i tried with .detach as well – Rodrigo Miranda Nov 03 '15 at 13:08
  • Are you sure `getFragmentManager().findFragmentByTag(Integer.toString(alarmNumber))` exists? Check with a debug log if it is null or an object. – Daniel Zolnai Nov 03 '15 at 13:49
  • Sorry for asking, but how do i do it? does the Log allows me to print a fragmentManager? – Rodrigo Miranda Nov 03 '15 at 14:54
  • Before this line: `getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentByTag(Integer.toString(alarmNumber))).commit();` Add this line: `System.out.println("The fragment is: " + getFragmentManager().findFragmentByTag(Integer.toString(alarmNumber))` Then check logcat output. – Daniel Zolnai Nov 03 '15 at 15:06
  • Yes, the fragment is null, i will be looking for a solution but if you have something already cooked, I'll be glad to hear it :) – Rodrigo Miranda Nov 05 '15 at 03:02
  • It's probably the tag: 1. Here you use amount: `Integer.toString(getAlarmsAmount())` 2. Here you use index: `findFragmentByTag(Integer.toString(alarmNumber))` – Daniel Zolnai Nov 05 '15 at 08:32
  • I tried using the same but with just Integer.toString(2) as an example, it is null as well maybe the issue is with the constructor of fragment manager? – Rodrigo Miranda Nov 05 '15 at 22:03
  • Make sure you use `getSupportFragmentManager()` everywhere – Daniel Zolnai Nov 06 '15 at 07:58