从下一个片段获取数据后如何将数据设置为片段的textview

时间:2019-01-25 10:45:43

标签: android textview fragment

从下一个片段接收数据后,

值不能在textview上显示。

步骤1:点击fragmentA的图像后,转到fragmentB 步骤2:从fragmentB返回后,我已经发送了一些数据 步骤3:在fragmentA中,onActivityResponse方法接收到数据,但textview.setText方法未将数据设置为textView

片段A

public void gotoFragmentB() {
    FragmentTransaction transaction =  getFragmentManager().beginTransaction();
    Fragment arEditorFragment = new AREditorFragment();
    arEditorFragment.setTargetFragment(PostNewJobThirdFragment.this, FRAGMENT_REQUEST_CODE);
    transaction.replace(R.id.container, arEditorFragment, getTag()); // give your fragment container id in first parameter
    transaction.addToBackStack(arEditorFragment.getClass().getName());  // if written, this transaction will be added to backstack
    transaction.commit();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == FRAGMENT_REQUEST_CODE && resultCode == RESULT_OK) {
  String jobDesc = data.getStringExtra("job_description");
  Log.v(TAG, "job description : " + jobDesc);
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  job_desc.setText(Html.fromHtml(jobDesc, Html.FROM_HTML_MODE_COMPACT));
  job_spec.setText(Html.fromHtml(jobDesc, Html.FROM_HTML_MODE_COMPACT));
        }
    }
}

片段B

public void goBackToFragmentA()
{
    Intent intent = new Intent(view.getContext(), AREditorFragment.class);
    intent.putExtra("job_description", mEditText.getHtml().toString());
    getTargetFragment().onActivityResult(getTargetRequestCode(), RESULT_OK,   intent);
getFragmentManager().popBackStack();
}

1 个答案:

答案 0 :(得分:1)

尝试这个解决方案。我遇到了类似的问题,我这样解决了它: 重新开始第一个片段,然后从堆栈中删除fragmentB

    public void goBackToFragmentA()
    {
        FragmentA fragment = new ResultFragment();
            Bundle bundle = new Bundle();
            bundle.putParcelable(ScanConstants.SCANNED_RESULT, uri);
            bundle.putString("selectedName",selectedName);
            bundle.putString("externalDirectory",externalDirectory);
            fragment.setArguments(bundle);
            android.app.FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.content, fragment);
            fragmentTransaction.addToBackStack(ResultFragment.class.toString());
            fragmentTransaction.commit();
getFragmentManager().beginTransaction().remove(FragmentB.this).commit();
    }