簡體   English   中英

未從片段調用 onActivityResult

[英]onActivityResult not called from fragment

我有一個包含BottomNavigationViewNavHostFragment的活動。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);

    bottomNavigationView.setSelectedItemId(R.id.navigation_profile);

    NavController navController = Navigation.findNavController(this, R.id.myNavHostFragment);
    NavigationUI.setupWithNavController(bottomNavigationView, navController); 

我還在這個父活動中添加了onActivityResult方法。

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Toast.makeText(this,"Parent onActivity",Toast.LENGTH_SHORT).show();
    for (Fragment fragment : getSupportFragmentManager().getFragments()) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

在片段中,我有一個ImageView ,我在其中使用 Intent 從用戶那里選擇圖像。

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
this.startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);

在片段的onActivityResult我這樣做:

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    Toast.makeText(getActivity(),"OnActivityResult",Toast.LENGTH_SHORT).show();

    // Result returned from launching the Intent to select image for profile
    if (requestCode == RC_PHOTO_PICKER && resultCode == Activity.RESULT_OK) {
        Toast.makeText(getActivity(),"Called",Toast.LENGTH_SHORT).show();
    }
}

問題是onActivityResult既未在父活動中調用,也未在片段中調用。

我曾嘗試在提供各種解決方案正確答案為ANSWER2ANSWER3 但他們都沒有工作!

您不能將Intent.createChooser()Intent.GET_CONTENT一起Intent.GET_CONTENT - 這對於活動和碎片都是如此。

相反,只需將您的Intent直接與startActivityForResult一起使用即可。

根據這個答案

更改

this.startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);

startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);

將以下代碼塊添加到父onActivityResult

for (Fragment fragment : getSupportFragmentManager().getFragments()) {
   //send result to all child fragment
       if(fragment!=this)
        fragment.onActivityResult(requestCode, resultCode, data);
    }

您與Intent一起使用的上下文可能指向其他內容。 在你的片段中嘗試這個

 private Fragment fragment;
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

  fragment = this;

 }

然后再次對你的片段

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
fragment.startActivityForResult(Intent.createChooser(intent, "Complete action using"), 
RC_PHOTO_PICKER);

不太確定,但片段是否可以調用意圖? 你可以嘗試在單擊imageView或按鈕時使用監聽器,然后攔截它並從父活動調用意圖嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM