onActivityResult() inside Nested Fragment is now called on Support Library rev 23.2 and above

Posted on 26 Mar 2016 19:46 | 55995 reads | 0 shares
 

One of the most annoying problem of Nested Fragment (a Fragment that is attached on another Fragment) is although we can call startActivityForResult(...) but onActivityResult(...) will not be called on the way back. You can find the reason behind in this blog post "How to make onActivityResult get called on Nested Fragment".

Previously if you want to make it work, you need to do some workaround, for example, NestedFragment introduced by me last year. Anyway good news is here. No any extra workaround is needed anymore since the problem has already been fixed with Fragment brought in Android Support Library rev 23.2 onwards. onActivityResult is now called in Nested Fragment !

Testing

I did some experiment by creating two types of Fragment: MainFragment and SecondFragment. MainFragment is placed on Activity while SecondFragment is placed on MainFragment like shown in diagram below.

SecondFragment is a very simple Fragment contains just a single button named btnGo with simple logic shown in code below.

// SecondFragment.java

// Calling to SecondActivity
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ...
    btnGo = (Button) rootView.findViewById(R.id.btnGo);
    btnGo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), SecondActivity.class);
            startActivityForResult(intent, 12345);
        }
    });
}

// Get Result Back
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d("onActivityResult", "requestCode = " + requestCode);
}

As you can see in the code shown above. When btnGo is clicked, SecondActivity will be launched by startActivityForResult  command. And here is the source code of SecondActivity.

// SecondActivity.java
public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
    }
}

SecondActivity does nothing but just simply immediately finish itself right after it is launched and result will be delivered back to the caller.

Expectation: When btnGo is clicked, onActivityResult inside SecondFragment must be called and a line of log must be shown inside logcat window.

And here is the result when tested with rev 23.1.1 and rev 23.2.1

With Android Support Library revision 23.1.1

com.inthecheesefactory.lab.nestedfragment I/OpenGLRenderer: Initialized EGL, version 1.4
com.inthecheesefactory.lab.nestedfragment W/EGL_emulation: eglSurfaceAttrib not implemented
com.inthecheesefactory.lab.nestedfragment W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaf853600, error=EGL_SUCCESS

With Android Support Library revision 23.2.1

com.inthecheesefactory.lab.nestedfragment I/OpenGLRenderer: Initialized EGL, version 1.4
com.inthecheesefactory.lab.nestedfragment W/EGL_emulation: eglSurfaceAttrib not implemented
com.inthecheesefactory.lab.nestedfragment W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaf853600, error=EGL_SUCCESS
com.inthecheesefactory.lab.nestedfragment D/onActivityResult: requestCode = 12345

As you can see, onActivityResult on SecondFragment is not called on Android Support Library rev 23.1 and below while it is called perfectly on rev 23.2 and above.

Personally this is a little big change which could change the way we code. Let's start switching to Android Support Library rev 23.2+ now and make your code more clean ! =D

Author: nuuneoi (Android GDE, CTO & CEO at The Cheese Factory)
A full-stack developer with more than 6 years experience on Android Application Development and more than 12 years in Mobile Application Development industry. Also has skill in Infrastucture, Service Side, Design, UI&UX, Hardware, Optimization, Cooking, Photographing, Blogging, Training, Public Speaking and do love to share things to people in the world!