Search code examples
androidandroid-fragmentsandroid-recyclerviewfragmentfragmenttransaction

android - I want to send data from recyclerview adapter from fragment to fragment opening new fragment


I want to send data from RecyclerView adapter to new Fragment. I succeeded in doing so, but why was the result of the collision in the previous fragment?

the least your attention is very big and meaningful to me :)

as shown below:

this is the replace result of the new fragment

I want the fragment to open up new fragments like WhatsApp, Instagram, etc.

RecyclerViewAdapter.java

package com.mimdudin.carekkerje.Adapter;

import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.mimdudin.carekkerje.Activities.HomeFragment;
import com.mimdudin.carekkerje.Activities.HomeFragmentDetail;
import com.mimdudin.carekkerje.Model.Job;
import com.mimdudin.carekkerje.R;

import java.util.List;


public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerViewAdapter.ViewHolder> {

    private Context context;
    private List<Job> jobList;

    public HomeRecyclerViewAdapter(Context context, List<Job> jobs){
        this.context = context;
        jobList = jobs;
    }

    @Override
    public HomeRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.home_row, parent, false);

        return new ViewHolder(view, context);
    }

    @Override
    public void onBindViewHolder(HomeRecyclerViewAdapter.ViewHolder holder, int position) {
    Job job = jobList.get(position);

    holder.title.setText(job.getTitle());
    holder.namaPerusahaan.setText(job.getNamaPerusahaan());
    holder.kriteria_1.setText(job.getKriteria_1());
    holder.kriteria_2.setText(job.getKriteria_2());
    holder.kriteria_3.setText(job.getKriteria_3());
    holder.gaji.setText(job.getGaji());
    holder.lokasi.setText(job.getLokasi());
    holder.tanggal.setText(job.getTanggal());

    String img_logoLink = job.getImg_logo();
        Glide.with(context)
                .load(img_logoLink) // URL
                .apply(new RequestOptions().placeholder(android.R.drawable.ic_dialog_info)
                                .error(android.R.drawable.ic_dialog_alert).centerCrop().dontAnimate()
//                         .centerCrop(), .crossFade(), .thumbnail(), .dontAnimate(), .dontTransform() BitmapTransformation(.circleCrop())
                )
                .into(holder.img_logo); //TARGET GAMBAR YANG NAK DIUBAH

    }

    @Override
    public int getItemCount() {
        return jobList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

         TextView title;
         TextView namaPerusahaan;
         TextView lokasi;
         TextView kriteria_1;
         TextView kriteria_2;
         TextView kriteria_3;
         TextView gaji;
         ImageView img_logo;
         TextView tanggal;


        public ViewHolder(final View itemView, final Context ctx) {
            super(itemView);
            context = ctx;
            title = itemView.findViewById(R.id.titleID);
            namaPerusahaan = itemView.findViewById(R.id.namaPerusahaanID);
            kriteria_1 = itemView.findViewById(R.id.satuTv);
            kriteria_2 = itemView.findViewById(R.id.duaTv);
            kriteria_3 = itemView.findViewById(R.id.tigaTv);
            gaji = itemView.findViewById(R.id.gajiID);
            lokasi = itemView.findViewById(R.id.lokasiID);
            img_logo = itemView.findViewById(R.id.img_logoID);
            tanggal = itemView.findViewById(R.id.tanggalID);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HomeFragmentDetail fragmentDetail = new HomeFragmentDetail();
                    Bundle bundle = new Bundle();
                    Job job = jobList.get(getAdapterPosition());
                    bundle.putSerializable("job", job);
                    fragmentDetail.setArguments(bundle);

                    AppCompatActivity activity = (AppCompatActivity) itemView.getContext();
                    android.support.v4.app.FragmentManager fragmentManager = activity.getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//          fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    fragmentTransaction.replace(R.id.frame_fragmenthome, fragmentDetail);
                    fragmentTransaction.commit();

////                    Intent intent = new Intent(context, HomeDetailActivity.class);
////                    intent.putExtra("job", job);
////                    ctx.startActivity(intent);

                }
            });
        }
    }

HomeFragmentDetail.java

package com.mimdudin.carekkerje.Activities;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mimdudin.carekkerje.Model.Job;
import com.mimdudin.carekkerje.R;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 */
public class HomeFragmentDetail extends Fragment {

    private TextView tvDeskripsi;
    private TextView tvIndustri;

    public HomeFragmentDetail() {
        // Required empty public constructor
    }

    public static HomeFragmentDetail newInstance(){
        HomeFragmentDetail homeFragmentDetail = new HomeFragmentDetail();
        return homeFragmentDetail;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home_fragment_detail, container, false);
        tvDeskripsi = view.findViewById(R.id.tvDeskripsi);
        tvIndustri = view.findViewById(R.id.tvIndustri);

        Bundle bundle = getArguments();
        Job job = (Job) bundle.getSerializable("job");

        String PostID = job.getId_post();
        String titleID = job.getTitle();

        tvDeskripsi.setText(PostID);
        tvIndustri.setText(titleID);

        return view;


//    DataHolder dataholder=new DataHolder("1","TestName");
//    Bundle bundle=new Bundle();
//    bundle.putSerializable("obj",dataholder);

//    Fragment fragment=new FragmentB();
//    fragment.setArguments(bundle);
//    fragmentManager = getActivity(). getSupportFragmentManager();
//    fragmentTransaction = fragmentManager .beginTransaction();
//    fragmentTransaction.add(R.id.container, fragment);
//    fragmentTransaction.commit();

        //Job job = (Job) getIntent.getSerializableExtra("job");

//        Bundle bundle = getArguments();
//        Job job = (Job) bundle.getSerializable("job");
//        String jobID = job.getId_post();
//        String titleID = job.getTitle();



    }

}

}

Solution

  • Solution 1:

    In the code where you replace HomeDetailFragment, call addToBackStack() like this

    fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.frame_fragmenthome, fragmentDetail);
    fragmentTransaction.addToBackStack(null);    // add this line to your code
    fragmentTransaction.commit();
    

    Here is a short explanation from android developer docs for Providing Proper Back Navigation.

    When there are FragmentTransaction objects on the back stack and the user presses the Back button, the FragmentManager pops the most recent transaction off the back stack and performs the reverse action (such as removing a fragment if the transaction added it).

    Solution 2:

    If there is no hard-and-fast requirement to use fragments, then I would recommend using HomeDetailActivity instead of HomeDetailFragment.

    Most of your above code will remain the same except you will be calling an Activity(via an intent) instead of a Fragment.

    This has certain advantages like you don't have to deal with fragmentManager or fragment backStack. In your case it's quite easy to implement.