Search code examples
androidlistviewandroid-studioandroid-fragmentsandroid-tabactivity

TabbarActivity fragment not called to onActivityResult


im using tabbaractivity and one of the tabs contains listView i want to update the listView every time that i adding object. to add object i do it from new activity what i wants is when this activity will close the listView will update by using adapter.notifydatachanged

in my activity that contains the tabs

   if (id == R.id.addButton) {
        Intent intent = new Intent(rootViewTabBarActivity.this, addPictureActivity.class);
        startActivityForResult(intent,101);
        return true;
    }

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

in the fragment (one of the tabs)

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if ((requestCode == 101) && (resultCode == Activity.RESULT_OK))
        adapter.notifyDataSetChanged();
}

and the activity that create the object and save him to database

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAM_REQUEST) {
        Bitmap btmp = (Bitmap)data.getExtras().get("data");
        takenPhoto.setImageBitmap(btmp);
    }
}
@Override
protected void onDestroy() {
    super.onDestroy();
    setResult(Activity.RESULT_OK);
}

Thanks


Solution

  • fragment not called to onActivityResult

    You miss passing the data from Activity to fragment In Activity, your code should be like this:

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == Activity.RESULT_OK) {
                YourFragment fragment = getFragment();
                if (fragment != null) {
                    // Pass data to Fragment
                    fragment.onActivityResult(requestCode, resultCode, data);
                }
            }
    
        }
    
    public YourFragment getFragment() {
            // You can find your fragment (by tag/ by id) here depends on your layout
        }