Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnActivityResult not called when nested fragment is using replace instead of add with Don't Keep Activity

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

like image 350
Elye Avatar asked Apr 20 '19 04:04

Elye


People also ask

How can we call fragment onActivityResult from activity?

To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity(). startActivityForResult(intent,111); inside your fragment. @StErMi Make sure you call startActivityForResult() and not getActivity(). startActivityForResult() from your fragment.

What is the use of onActivityResult in Android?

When you done with the subsequent activity and returns, the system calls your activity's onActivityResult() method. This method includes three arguments: @The request code you passed to startActivityForResult() . @A result code specified by the second activity.

What can I use instead of startActivityForResult in Kotlin?

In this article, we will discuss the Activity Result API, an alternative to the deprecated startActivityForResult + onActivityResult methods. We will use Kotlin in this article for the code samples. Android introduced the Activity Result APIs as a major change in the androidx. activity:activity:1.2.

What is onActivityResult?

onActivityResult is the callback you have on the first activity to grab the contacts you choose. Follow this answer to receive notifications.


1 Answers

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()
}
like image 125
Elye Avatar answered Oct 24 '22 10:10

Elye