0

I am trying to embed a Fragment in either a Dialog or DialogFragment

public class addAccountDialog extends DialogFragment 
{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.add_account_dialog, container);
    accountType.setOnItemSelectedListener(new OnItemSelectedListener() 
        {
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
            {
            passwordFragment newFragment = new passwordFragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_base, newFragment);
            transaction.commit();
            }
        }
    }

}



        <LinearLayout
            android:id="@+id/fragment_base"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="3" > 
        </LinearLayout>  

I have tried this using with both a Dialog and DialogFragmentto no avail. Has anyone been able to implement a fragment inside a dialog.

thank you in advance.

Roger

  • Why do you need to do this? I don't believe it is possible, as the fragment manager is from the activity class. Maybe if we knew what you are trying to accomplish, we could offer other suggestions. – Barak Aug 07 '12 at 19:31
  • You CAN nest Fragments, but it's not advisable: [http://stackoverflow.com/questions/6221763/android-can-you-nest-fragments][1] [1]: http://stackoverflow.com/questions/6221763/android-can-you-nest-fragments – Christopher Perry Aug 07 '12 at 22:01

1 Answers1

1

You aren't able to put Fragments within other Fragments.

My suggestion would be to put your Fragments within an Activity like normal and then give the Activity a Dialog theme by doing this in the XML:

<activity android:theme="@android:style/Theme.Dialog" />

edit: just a note, as of API-17 (4.2) you can now nest Fragments: Nested Fragments

telkins
  • 10,440
  • 8
  • 52
  • 79
  • Thank you Atlos for your timely response and the code, I will try the Theme.Dialog and see how it works and, if it provides the experience that I am looking for. – user1582832 Aug 08 '12 at 13:47