2016-12-21 96 views
1

我一直試圖在片段內顯示DialogFragment而沒有成功。在這裏我的代碼:在片段內顯示DialogFragment

@Override 
public void onResume() { 
    super.onResume(); 
    Log.d(TAG, "onResume"); 
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 
    if (user != null && !user.isEmailVerified()) { 
     InfoDialogFragment dialog = new InfoDialogFragment(); 
     dialog.show(getActivity().getSupportFragmentManager(), "InfoDialogFragment"); 
    } 
} 

每當片段是在屏幕上,屏幕變暗,當我觸摸屏幕它將恢復正常的對比度,但我不能看到對話框片段。 我錯過了什麼?

下面是DialogFragment代碼:

public class InfoDialogFragment extends DialogFragment { 
private InfoDialogListener listener; 

@Override 
public void onAttach(Context activity) { 
    super.onAttach(activity); 
    // Verify that the host activity implements the callback interface 
    try { 
     // Instantiate the InfoDialogListener so we can send events to the host 
     listener = (InfoDialogListener) activity; 
    } catch (ClassCastException e) { 
     // The activity doesn't implement the interface, throw exception 
     throw new ClassCastException(activity.toString() 
       + " must implement DeleteListListener"); 
    } 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // Use the Builder class for convenient dialog construction 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    // Get the layout inflater 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 
    View layout = inflater.inflate(R.layout.fragment_info, null); 

    TextView message = (TextView) layout.findViewById(R.id.message); 
    message.setText(getActivity().getString(R.string.user_not_verified)); 
    // 
    Button ok = (Button) layout.findViewById(R.id.ok_button); 
    ok.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      listener.onDialogOKClick(InfoDialogFragment.this); 
     } 
    }); 
    return builder.create(); 
} 


public interface InfoDialogListener { 
    void onDialogOKClick(DialogFragment dialog); 

} 

}

感謝

+0

你應該只是有一個功能,顯示在該片段中的呼叫活動的對話片段 - 使用FragmentInteractionListener模式,谷歌放入其樣板碎片/活動代碼。聽起來就像它試圖顯示對話框,雖然 –

+0

把InfoDialogFragment代碼在這裏 –

+1

我們可以看到InfoDialogFragment代碼? – Gudin

回答

3

使用getChildFragmentManager,而不是getActivity().getSupportFragmentManager()。這些是不同的例子。

+0

這很關鍵。謝謝。從片段我們應該使用childFragmentManager – j2emanue

0

事實上,我錯過了InfoDialogFragment類中的一行代碼。

builder.setView(layout); 

我花了2天才弄明白。 感謝您的回答。

0

您可以在InfoDialogFragment類中創建一個構造函數,你能證明它是這樣的:

DialogFragment newFragment = new SelectDateFragment(NextDate); 
newFragment.show(getFragmentManager(), "DatePicker");