안드로이드 fragment 데이터 전달 질문드립니다!!

조회수 1330회

메인액티비티에서

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        String comment = data.getStringExtra("comment");
        int rating = Math.round(data.getFloatExtra("rating", 0.0f));
        if (comment != null) {
            Bundle bundle = new Bundle();
            bundle.putString("comment",comment);
            bundle.putInt("rating", rating);
            movieDetailFragment.setArguments(bundle);
        }
    }
}

이런식으로 데이터를 보내고 받는 fragment에서 Listview갱신을 위해 이런식으로 코드를 짯는데 갱신이 되질 않습니다 문제가 있을까요~?

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.movie_detail_fragment, container, false);

    Bundle bundle1 = getArguments();
    if (bundle1 != null) {
        String comment = bundle1.getString("comment");
        int rating = bundle1.getInt("rating");
        items.add(new CommentItems("kim78**", rating, comment, R.drawable.user1));
        adapter.notifyDataSetChanged();
    }
  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • Activity 에서 해당 Fragment 의 인스턴스를 갖고 있다면 Activity 내의 onActivityResult() 가 호출 되면 해당 Fragment 의 메소드를 바로 실행해주면 될것 같네요. 또한 Fragment 에서도 onActivityResult() 를 override 하여 구현할수 있습니다

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)