简体   繁体   中英

Commit fragment into DialogFragment

Hello I have a problem with commit fragment into DialogFragment I have simple dialogFragment...

public class CustomCalendarDialog extends DialogFragment {

    public CustomCalendarDialog() {
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_custom_calendar, container);
        FrameLayout frame = view.findViewById(R.id.Dialogcontainer1);
        int id = frame.getId();

        FragmentManager fragmentManager = ((FragmentActivity) getActivity()).getSupportFragmentManager();
        Fragment customCalendarFragment = CustomCalendarFragment.newInstance();
        fragmentManager.beginTransaction().replace(id, customCalendarFragment );

        return view;
    }

}

And have Fragment which can commit into activity with this lines

Fragment customCalendarFragment = CustomCalendarFragment.newInstance();
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(frameLayout.getId(), customCalendarFragment ).commit();

with this lines in activity program work good.

Now I need replace this fragment into Dialog/Dialog Fragment.

I have layout fragment_custom_calendar.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000ff">
    <FrameLayout
        android:id="@+id/Dialogcontainer1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</FrameLayout>

Inflate layout into dialog

View view = inflater.inflate(R.layout.fragment_custom_calendar, container);

variable container is null I don't know if it's ok or no.

View it's ok, On screen can see Blue layout (#0000ff) But I want see my fragment (Custom calendar)

Program not crashed on commit lines

FrameLayout frame = view.findViewById(R.id.Dialogcontainer1);
    int id = frame.getId();
    FragmentManager fragmentManager = ((FragmentActivity) getActivity()).getSupportFragmentManager();
    Fragment customCalendarFragment  = CustomCalendarFragment.newInstance();
    fragmentManager.beginTransaction().replace(id, customCalendarFragment );

frame is not null id is not null customCalendarFragment is not null but I cant see it.

I create fragment :

public static Fragment newInstance() {
    return (Fragment) new CustomCalendarFragment();
}

My calling dialog from MainActivity

CustomCalendarDialog cdd = new CustomCalendarDialog();
    cdd.show(getFragmentManager(), "TAG");

I need this fragment in dialog :( I don't know if commit fragment is posible in normal Dialog if yes please link me and i will look.

Thanks any idea

I solve my similar problem with activity like here.

https://stackoverflow.com/a/18234691/8250313

    /* Start Activity */ public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setClassName("com.thinoo.ActivityTest", "com.thinoo.ActivityTest.NewActivity");
    startActivityForResult(intent,90); } /* Called when the second activity's finished */ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
    case 90:
        if (resultCode == RESULT_OK) {
            Bundle res = data.getExtras();
            String result = res.getString("param_result");
            Log.d("FIRST", "result:"+result);
        }
        break;
    } }

private void finishWithResult() {
    Bundle conData = new Bundle();
    conData.putString("param_result", "Thanks Thanks");
    Intent intent = new Intent();
    intent.putExtras(conData);
    setResult(RESULT_OK, intent);
    finish(); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM