6

I created a simple App below with

MainActivity -> ContainerFragment -> MainFragment

The ContainerFragment uses replace to commit the MainFragment.

I turn ON doesn't keep Activity`

When MainFragment call startActivityForResult, and return from the activity, the onActivityResult is not called.

This doesn't happen when

  • It is a single Fragment. (not nested)
  • Don't Keep Activity is OFF
  • When ContainerFragment uses add to commit the MainFragment.

To replicate the problem, download the code in the following github and run it. (Remember to turn on Don't Keep Activity). Click Start Activity.., and then click End Activity and expect a Toast (no Toast, shows that the onActivityResult in the fragment is not called)

https://github.com/elye/issue_android_onactivityresult_not_called

Apparently there was previously a reported nested Fragment can't get onActivityResult in the past as posted in https://stackoverflow.com/a/36239436/3286489, but stated has been resolved in Android Support Library 23.2.1. But for my case, I tried on Android Support Library 28 (Android Pie). So this is still a valid issue I believe.

Share it here in case I am wrong, and there's some explanation to this out there.

In case workaround, there's one recommended in https://inthecheesefactory.com/blog/how-to-fix-nested-fragment-onactivityresult-issue/en.

Another workaround as reported in https://stackoverflow.com/a/20543245/3286489

Elye
  • 53,639
  • 54
  • 212
  • 474
  • are you expecting the fragment's `onActivityResult` being called or the activity's `onActivityResult` to being called? – Luca Nicoletti Apr 20 '19 at 06:35
  • The `onActivityResult` in the Fragment is not being called though expected. It is called in the Activity's `onActivityResult`, but doesn't have the matching `requestCode`. – Elye Apr 21 '19 at 10:14
  • if you want to have the right `resultCode` use `activity.startActivityForResult` in the fragment – Luca Nicoletti Apr 21 '19 at 10:18
  • No, we want the `onActivityResult` called in the triggering (nested) fragment (commit with `replace` operation). It works when `Don't Keep Activity` is OFF, but not when it is ON. – Elye Apr 21 '19 at 10:22
  • Discovered it is still a bug out there even in AndroidX https://issuetracker.google.com/issues/130327691 – Elye Apr 21 '19 at 10:23

2 Answers2

2

Based on the answer from https://issuetracker.google.com/issues/36926548, it solved the problem.

The reason is before the Fragment is created, we should check that savedStateInstace is null first.

In this case in the project of https://github.com/elye/issue_android_onactivityresult_not_called,

the ContainerFragment it should be

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    if (savedInstanceState == null)
        childFragmentManager.beginTransaction().replace(R.id.fragment_container, MainFragment()).commit()
}

And in MainActivity, it should be

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    if (savedInstanceState == null)
        supportFragmentManager.beginTransaction().add(R.id.container, ContainerFragment()).commit()
}
Elye
  • 53,639
  • 54
  • 212
  • 474
  • Perfect - thank you! I spent a day debugging this issue. This seems like really strange behavior. – Alex Sullivan Mar 31 '21 at 18:54
  • Programming with Fragment is hard as usual. I have written various issue one could fall into in https://medium.com/mobile-app-development-publication/7-common-mistakes-easily-made-with-android-fragment-6fc85c44e783?sk=708bebf4db63882915115700b0f1c9ae – Elye Apr 01 '21 at 11:30
0

I had this issue, that Fragment.onActivityResult() has not been called, when I tried to call from within a fragment:

getActivity().startActivityForResult()

instead of

this.startActivityForResult()
Malachiasz
  • 7,126
  • 2
  • 35
  • 49