In my Activity (that extends android.support.v7.app.ActionBarActivity) I have a fragment that is normally include into my views like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fragment
android:name="com.fragments.SomeFragment"
android:id="@+id/fragment_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
That works well.
But I've decided to reuse this fragment as a dialog, so I extended it by android.support.v4.app.DialogFragment (from supported library).
Without any other changes I get the exception
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ExampleActivity}: java.lang.IllegalStateException: DialogFragment can not be attached to a container view
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: DialogFragment can not be attached to a container view
at android.support.v4.app.DialogFragment.onActivityCreated(DialogFragment.java:364)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1486)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552)
at com.*.RoboActionBarActivity.onStart(RoboActionBarActivity.java:58)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
at android.app.Activity.performStart(Activity.java:5114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
... 11 more
How can I resolve this problem or use my standard fragment as a dialog?
My Activity:
public class SomeActivity extends RoboActionBarActivity{
private SomeFragment someFragment;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actvity_some);
this.someFragment = (SomeFragment) getSupportFragmentManager().findFragmentById(
R.id.fragment_id);
}
}
RoboActionBarActivity is a ActionBarActivity that implements RoboContext (from RoboGuice) to enable injection.
public class RoboActionBarActivity extends ActionBarActivity implements RoboContext {
private EventManager eventManager;
private final HashMap<Key<?>, Object> scopedObjects = new HashMap<Key<?>, Object>();
@Inject
private ContentViewListener ignored; // BUG find a better place to put this
@Override
protected void onCreate(final Bundle savedInstanceState) {
final RoboInjector injector = RoboGuice.getInjector(this);
this.eventManager = injector.getInstance(EventManager.class);
injector.injectMembersWithoutViews(this);
super.onCreate(savedInstanceState);
this.eventManager.fire(new OnCreateEvent(savedInstanceState));
}
@Override
protected void onRestart() {
super.onRestart();
this.eventManager.fire(new OnRestartEvent());
}
@Override
protected void onStart() {
super.onStart();
this.eventManager.fire(new OnStartEvent());
}
@Override
protected void onResume() {
super.onResume();
this.eventManager.fire(new OnResumeEvent());
}
@Override
protected void onPause() {
super.onPause();
this.eventManager.fire(new OnPauseEvent());
}
@Override
protected void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
this.eventManager.fire(new OnNewIntentEvent());
}
@Override
protected void onStop() {
try {
this.eventManager.fire(new OnStopEvent());
} finally {
super.onStop();
}
}
@Override
protected void onDestroy() {
try {
this.eventManager.fire(new OnDestroyEvent());
} finally {
try {
RoboGuice.destroyInjector(this);
} finally {
super.onDestroy();
}
}
}
@Override
public void onConfigurationChanged(final Configuration newConfig) {
final Configuration currentConfig = getResources().getConfiguration();
super.onConfigurationChanged(newConfig);
this.eventManager.fire(new OnConfigurationChangedEvent(currentConfig, newConfig));
}
@Override
public void onContentChanged() {
super.onContentChanged();
RoboGuice.getInjector(this).injectViewMembers(this);
this.eventManager.fire(new OnContentChangedEvent());
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
this.eventManager.fire(new OnActivityResultEvent(requestCode, resultCode, data));
}
@Override
public Map<Key<?>, Object> getScopedObjectMap() {
return this.scopedObjects;
}
}