2016-03-08 100 views
1

我想要顯示Dialog以便在Fragment之間打開Camera/Gallery。一個Button按下Android onActivityResult永遠不會從片段中的DialogFragment調用

後,我顯示自定義DialogFragment(這DialogFragmentinnerclassFragment內)。被選擇的一個opcion

public static class AddPictureDialogFragment extends DialogFragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_add_picture_dialog, container, false); 
     getDialog().setTitle("New Picture"); 

     Button openCameraButton = (Button) rootView.findViewById(R.id.open_camera_button); 
     openCameraButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("","Open Camera Option Selected"); 
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(intent, REQUEST_CAMERA); 
       getDialog().dismiss(); 

      } 
     }); 

     Button openGalleryButton = (Button) rootView.findViewById(R.id.open_gallery_button); 
     openGalleryButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("","Open Gallery Option Selected"); 
       Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       intent.setType("image/*"); 
       startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE); 
       getDialog().dismiss(); 
      } 
     }); 

     return rootView; 
    } 

} 

後,Camera/Gallery被初始化,但撥打/選擇畫面之後,該方法onActivityResult不會被調用。

下面是代碼,我創建DialogFragment

FragmentManager fm = getActivity().getSupportFragmentManager(); 
    AddPictureDialogFragment addPictureDialogFragment = new AddPictureDialogFragment(); 
    addPictureDialogFragment.show(fm, getTag()); 

奇怪的是,如果我直接在Fragment創建DialogFragment不使用它的工作DialogFragment ...

我有當創建DialogFragment時也嘗試過:

addPictureDialogFragment.setTargetFragment(this, 1); 

and thi S,初始化的cameraIntent時:

Button openCameraButton = (Button) rootView.findViewById(R.id.open_camera_button); 
    openCameraButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.d("","Open Camera Option Selected"); 
      getDialog().dismiss(); 

      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      getTargetFragment().onActivityResult(getTargetRequestCode(), 1, intent); 
      startActivityForResult(intent, REQUEST_CAMERA);   
      } 
    }); 

回答

1

試試這個:

private void showAddImageDialog() { 
    FragmentManager fm = getActivity().getSupportFragmentManager(); 
    AddPictureDialogFragment addPictureDialogFragment = new AddPictureDialogFragment(); 
    addPictureDialogFragment.setTargetFragment(this, REQUEST_CAMERA); 
    addPictureDialogFragment.show(fm, getTag()); 
} 



public static class AddPictureDialogFragment extends DialogFragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_add_picture_dialog, container, false); 
     getDialog().setTitle("New Picture"); 


     Button openCameraButton = (Button) rootView.findViewById(R.id.open_camera_button); 
     openCameraButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("","Open Camera Option Selected"); 
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       getTargetFragment().startActivityForResult(intent, getTargetRequestCode()); 
       getDialog().dismiss(); 
      } 
     }); 

     Button openGalleryButton = (Button) rootView.findViewById(R.id.open_gallery_button); 
     openGalleryButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("","Open Gallery Option Selected"); 
       Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       intent.setType("image/*"); 
       getTargetFragment().startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE); 
       getDialog().dismiss(); 
      } 
     }); 

     return rootView; 
    } 

} 
+0

那個作品!謝謝! – Ale

1

在您的主要活動覆蓋onActivityResult方法如下。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    for (Fragment fragment : getSupportFragmentManager().getFragments()) { 
    if(fragment!=null) 
     fragment.onActivityResult(requestCode, resultCode, data); 
    } 
} 

有時片段可能被破壞,但引用可能會持續,因此if(fragment!= null)檢查。

+0

感謝@肆虐h Ramesh。是的,我知道,但我希望它在'片段'中,而不是'活動' – Ale

+0

對不起,我沒有注意到這一點。更新我的答案 –

+0

它工作99%。 'onActivityResult'現在在'Fragment'中被調用,但'requestCode'是錯誤的。無論如何,我投了你的答案,謝謝你的時間和你的努力:) – Ale

1

請在您的主要活動中編寫此代碼。

// In your activity 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     System.out.println("onActivityResult Main Activity"); 
     for (Fragment fragment : getSupportFragmentManager().getFragments()) { 
      fragment.onActivityResult(requestCode, resultCode, data); 
     } 
    } 
1

在父活動中覆蓋onActivityResult即所有片段

覆蓋在您的主要活動此方法的父:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    TestFrag demoFragment = (TestFrag) getSupportFragmentManager().findFragmentByTag("test"); 
    demoFragment.onActivityResult(requestCode, resultCode, data); 
} 

您的片段活動結果的呼叫

2

這部分後是很簡單的

只需撥打startActivityForResult(intent,constant); 你片段類

覆蓋onActivityResult的活動並且不刪除super.onActivityResult(requestCode, resultCode, data);

也覆蓋片段類onActivityResult

在活動

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    Log.d(TAG, "activity result called"); 
    super.onActivityResult(requestCode, resultCode, data); 
} 

在片段

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    Log.d(TAG, "fragment result called"); 
    super.onActivityResult(requestCode, resultCode, data); 
    if(requestCode==constant){ 
     Logger.d(TAG, "got result"); 
    } 
} 
相關問題