TabbarActivity片段未调用onActivityResult

时间:2017-03-31 02:45:24

标签: android listview android-studio android-fragments android-tabactivity

即时通讯使用tabbaractivity,其中一个标签包含listView 我想在每次添加对象时更新listView。 添加对象我是从新活动中做到的 我想要的是当这个活动将关闭listView将使用更新 adapter.notifydatachanged

在我的活动中包含标签

   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);
}
片段中的

(其中一个标签)

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

以及创建对象并将其保存到数据库的活动

@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);
}

由于

1 个答案:

答案 0 :(得分:1)

  

片段未调用onActivityResult

您错过了将数据从Activity传递到片段 在Activity中,您的代码应该是这样的:

@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
    }