Search code examples
androidandroid-fragmentsfragmenttransaction

How to remove the layout of the first fragment after transaction to another fragment?


I want to redirect a user from one fragment to another, but the problem is that I have the 2 fragments layouts on top of each other. So I have a first fragment containing a recycler view as shown below : I want to redirect the user to another fragment when he presses on a text in the recycler view.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/nav_admin_seller"
tools:context=".Admin.Seller.AdminSellerFragment">

<TextView
    android:id="@+id/tv_date_admin_seller"
    android:layout_width="wrap_content"
    android:layout_height="25dp"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:textColor="@color/logo_color"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_admin_seller"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="48dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tv_date_admin_seller" />

</FrameLayout>

This is the part of the Adapter where I implement the onClickListenener

@RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onBindViewHolder(AdminSellerAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {

        AdminSeller item = itemList.get(position);
        holder.name.setText(String.valueOf(item.getUsername()));
        holder.password.setText(String.valueOf(item.getPassword()));
        holder.daliycash.setText(String.valueOf(item.getDaliyCash()));
        holder.totalcash.setText(String.valueOf(item.getTotalCash()));


        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("call.myfragment.action");
                id = String.valueOf(itemList.get(position).getSellerId())+"/"+current_date;
                intent.putExtra("id",id);
                mCtx.sendBroadcast(intent);
            }
        });

    }

Then I add a broadcast receiver in the MainActivity containing the 2 fragments:

BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String id = intent.getStringExtra("id");
                Bundle arguments = new Bundle();
                arguments.putString( "id" , id);

                //arguments.putBoolean("cameFromSeller1",true);
                Fragment frag = null;
                frag = new AdminSellerMoreInfo();
                frag.setArguments(arguments);
                //Fragment fragment = new AdminSellerFragment();
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.nav_admin_seller, frag);
                //ft.detach(fragment);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.addToBackStack(null);
                ft.commit();
            }
        };
        Admin.this.registerReceiver(mBroadcastReceiver, new IntentFilter("call.myfragment.action"));

And this is the Java code of the second fragment:

public class AdminSellerMoreInfo extends Fragment {
    View root;
    private String sellerid;

    private List<SellerOutStockItem> itemList1 = new ArrayList<>();
    private RecyclerView recyclerView1;
    private ADMINSELLERADAPTER_ITEM_QUENTITY adapter1;

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


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        root = inflater.inflate(R.layout.fragment_admin_seller_more_info, container, false);
        recyclerView1 = root.findViewById(R.id.rv_admin_seller_items_name_q);
        Bundle arguments = getArguments();
        if (arguments != null) {
            sellerid = arguments.getString("id");
            String id1 = sellerid;
        }
        String[] split = sellerid.split("/");
        getitems_name(split[0],split[1]);
        return root;
    }
}

And this is the layout of the second fragment

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/nav_admin_seller2"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_admin_seller_items_name_q"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</FrameLayout>

Solution

  • In the onReceive() method of broadcast receiver in MainActivity you wrote-

     ft.replace(R.id.nav_admin_seller, frag);
    

    The first argument is the id, here you should put the id of the root layout in your main_activity.xml which is the actual container of these fragments while performing fragment transactions.