Android onActivityResult in fragment
Asked Answered
D

4

6

I try to get the result in a fragment. Code in fragment

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode != Activity.RESULT_OK) return

    val bundle = data?.extras
    when (requestCode) {
        LicenseActivationActivity.REQUEST_RESULT_COUNT_ENTERPRISE ->
     //
    }
}

This code is called. But also method onActivityResult in DrawerActivity

Do that onActivityResult is called only in the fragment?

UPDATE

This code in DrawerActivity

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK) {
        WayBillsActivity.start(this)
        finish()
    }
}

For some reason, it is also satisfied, but only after onActivityResult in fragment.

Duel answered 16/8, 2018 at 12:24 Comment(4)
Possible duplicate of onActivityResult is not being called in FragmentDejecta
first it always call activity onActivityResult and from that you have to call fragment onActivityResultWomankind
remove super from method.Moats
Yes. I do not understand where this event comes from. Value requestCode in DrawerActivity is not used anywhere in the application.Duel
Y
6

in your Activity call this

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

Then call in fragment

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  //super.onActivityResult(requestCode, resultCode, data); comment this unless you 

 }
Yearning answered 16/8, 2018 at 12:51 Comment(2)
It worked for me correctly! I used this in kotlin. Source code was like this: override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) for (fragment in supportFragmentManager.fragments) { fragment.onActivityResult(requestCode, resultCode, data) } }Biotechnology
This is working for meTerra
I
2

add code in Drawer activity

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
   fragment.super.onActivityResult(requestCode, resultCode, data)

}

   **fragment.super.onActivityResult(requestCode, resultCode, data)**

Then your onActivityResult method of fragment called

Isola answered 16/8, 2018 at 12:28 Comment(0)
K
0

Everything seems good in the code. Have you checked with the debugger because I have implemented the same in my fragment and it's working fine

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

        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQ_CODE_EDIT_PROFILE:
                    userModel = data.getExtras().getParcelable(UserModel.class.getSimpleName());
                    setData();
                    break;
            }
        }
    }
Koralle answered 16/8, 2018 at 12:40 Comment(0)
S
0

onActivityResult in fragment is called only after such method in activity. If you want handle result only on activity or only in fragment - maybe it will be better to use different result codes for different situation (if I understand your problem correctly)

Shutout answered 16/8, 2018 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.