2015-12-11 67 views
1

我想在創建片段後立即顯示AlertDialog(帶有幾個單選按鈕按鈕和一個OK按鈕)。如何在創建片段後立即顯示AlertDialog

哪裏是調用對話框片段的最佳位置?我嘗試過onViewCreated()和onResume(),兩者都可以工作,但我不確定最佳做法。

此外,爲了確保每次片段停止/重新創建時都不顯示對話框,例如,屏幕旋轉,我創建了一個名爲mShowDialog的布爾值,並在onCreate()中將其設置爲'true',然後使用'If'語句來決定是否顯示對話框(例如參見下文)。

onCreate(){ 
    //.... 
    mShowDialog = true; 
} 

onResume(){ 
    if (mShowDialog){ 
     //....show dialog code 
     // set mShowDialog to false to ensure code executed only once 
     mShowDialog = false; 
    } 
} 

上述代碼是滿足這兩個要求的最佳方式嗎?

順便說一句,我是相當新的編程。

在此先感謝您的幫助。

回答

1

對此的最佳做法是在onCreateView()片段方法中擴大對話框。

0

如果您正在嘗試通過添加片段的活動來創建該片段,我已將祝賀片添加到FragmentListener並從活動中進行設置。這是我的基本BaseFragment類,我所有的片段擴展:

public class BaseFragment extends Fragment { 

    public Context context; 
    public Activity activity; 

    public FragmentListener fragmentListener; 

    private boolean attached = false; 

    public BaseFragment() { 

    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     if (!isAttached()) { 
      this.context = activity; 
      this.activity = activity; 

      if (this.fragmentListener != null) { 
       this.fragmentListener.onAttached(); 
      } 

      setAttached(true); 
     } 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 

     if (!isAttached()) { 
      this.context = context; 
      this.activity = (Activity) context; 

      if (this.fragmentListener != null) { 
       this.fragmentListener.onAttached(); 
      } 

      setAttached(true); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 

     setAttached(false); 
     if (this.fragmentListener != null){ 
      this.fragmentListener.onDetached(); 
     } 
    } 

    public void setFragmentListener(FragmentListener fragmentListener) { 
     this.fragmentListener = fragmentListener; 
    } 

    public View.OnClickListener onBackTapped = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      getActivity().onBackPressed(); 
     } 
    }; 

    public boolean isAttached() { 
     return attached; 
    } 

    public void setAttached(boolean attached) { 
     this.attached = attached; 
    } 

    public boolean isPermissionGranted(String permission){ 
     return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; 
    } 

    public boolean ifShouldShowRationaleForPermission(String permission){ 
     return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission); 
    } 

    public void showPermissionRequest(Activity activity, int requestCode, String... permissions){ 
     ActivityCompat.requestPermissions(activity, permissions, requestCode); 
    } 
} 

這樣一來,我可以在我的活動做到這一點:

MyFragment myFragment = new MyFragment(); 
myfragment.setFragmentListener(new FragmentListener() { 
    @Override 
    public void onAttached() { 
     // Stuff I want to do when it is attached 
    } 

    @Override 
    public void onDetached() { 
     // Stuff I want to do when it is detached 
    } 
}); 

fragmentManager.beginTransaction() 
     .add(R.id.container, myFragment) 
     .commit(); 

然後我可以加我想要的任何代碼時,碎片也它是各種各樣的東西。

祝你好運!

相關問題