0

i have already searched this at other similar questions but i could not solve it. My problem is that i can not open a fragment from another fragment, i'm doing this:

FragmentLugaresLista frag = new FragmentLugaresLista();
Bundle args = new Bundle();
args.putString("gastronomyElementType", "Restaurants");
frag.setArguments(args);

ActivityContenedor asd = new ActivityContenedor();
asd.openFragment(frag);

the method openFragment into ActivityContenedor:

public void openFragment(Fragment fragmentToOpen) {
    FragmentManager manager = getFragmentManager();
    manager.beginTransaction()
    .replace(R.id.content_frame, fragmentToOpen).commit();
    mDrawerLayout.closeDrawer(mDrawerList);
}

and i'm having this error:

11-04 16:23:38.731: E/AndroidRuntime(20705): FATAL EXCEPTION: main
11-04 16:23:38.731: E/AndroidRuntime(20705): java.lang.IllegalStateException: Activity     has been destroyed
11-04 16:23:38.731: E/AndroidRuntime(20705):    at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1299)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at android.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at android.app.BackStackRecord.commit(BackStackRecord.java:525)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at com.example.despegarteproject.ActivityContenedor.openFragment(ActivityContenedor.java:113)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at com.example.despegarteproject.FragmentLugares$GridViewListener.onItemClick(FragmentLugares.java:242)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at android.widget.AbsListView.performItemClick(AbsListView.java:1068)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2525)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at android.widget.AbsListView$1.run(AbsListView.java:3186)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at android.os.Handler.handleCallback(Handler.java:605)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at android.os.Looper.loop(Looper.java:137)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at android.app.ActivityThread.main(ActivityThread.java:4448)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at java.lang.reflect.Method.invokeNative(Native Method)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at java.lang.reflect.Method.invoke(Method.java:511)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
11-04 16:23:38.731: E/AndroidRuntime(20705):    at dalvik.system.NativeStart.main(Native Method)

could you help me? i would appreciate very much.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • It looks like your destroying the parent activity but i dpn't have your full code. Fragments mimic the lifecycle of their parent activity in that if the parent is destroyed all child fragments will also be destroyed causing this illegal state exception. What is happening in your closeDrawer method? – kandroidj Nov 04 '13 at 20:04

3 Answers3

1

It looks like you are creating a new activity and trying to add a fragment on it, but you are only using it's public constructor and not any of the "startActivity" methods from the Context class.

This activity has not being created by the system and has not went through all the activity life cycle. I assume "destroyed" is the default state in which activities are created, and this state is checked when you try to add a fragment to them.

1

If your fragments are only going to exist as children of an ActivityContenedor, you could do this:

Activity parent = getActivity();
if (parent != null 
    && !parent.isFinishing() 
    && parent instanceof ActivityContenedor) {
    FragmentLugaresLista frag = new FragmentLugaresLista();
    Bundle args = new Bundle();
    args.putString("gastronomyElementType", "Restaurants");
    frag.setArguments(args);

    ((ActivityContenedor) parent).openFragment(frag);
}

instead of creating a 'new' activity (which is never the correct way to create an Activity).

This acquires a reference to the Activity that this fragment is attached to, casts it to your ActivityContenedor class, and then you've got a valid instance you can call your openFragment method on.

Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
Adam S
  • 16,144
  • 6
  • 54
  • 81
1
  1. Are you trying to launch a new activity with a new Fragment in it?

  2. Are you trying to replace an existing Fragment in an existing activity?

  3. What are you trying to do? :)

Never create a new activity with Activity a = new Activity();

With that being said, the ideal method to go from one Fragment to another depends whether you want to change the activity or not…

If you can change it, then simply pass a value via Intent to the activity so it can launch a fragment when it's created.

If you don't want to change the activity, simply perform a FragmentTransaction to replace the Fragment.

Don't make things complicated, they shouldn't be in this case :)

Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144