Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android onActivityResult in fragment

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.

like image 385
user1854307 Avatar asked Aug 16 '18 12:08

user1854307


2 Answers

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 

 }
like image 53
Syed Danish Haider Avatar answered Nov 15 '22 00:11

Syed Danish Haider


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

like image 45
Chirag Nahar Avatar answered Nov 15 '22 00:11

Chirag Nahar