First Fragment is not calling after button click from Activity android

I have two buttons. Fragment A and Fragment B class to call.

In Activity, I used Fragment A to call after button click. I get toast message but Fragment A is not calling.

MainActivity.java:

import android.app.Activity;
import android.app.Dialog;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnFragmentOne= findViewById(R.id.fragment_button_2);
       
        Button btnFragmentTwo = findViewById(R.id.fragment_button_1);

        btnFragmentOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"First button",Toast.LENGTH_SHORT)
                        .show();
                FirstFragment regcomplainfragment = new FirstFragment();
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                transaction.replace(R.id.content_frame, regcomplainfragment);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });

        btnFragmentTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction fragmenttransaction = getFragmentManager().beginTransaction();
                SecondFragment regcomplainfragment = new SecondFragment();
                fragmenttransaction.add(R.id.content_frame, regcomplainfragment);
                fragmenttransaction.commit();
            }
        });
    } Button btnFragmentTwo = findViewById(R.id.fragment_button_1);

        btnFragmentOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"First button",Toast.LENGTH_SHORT)
                        .show();
                FirstFragment regcomplainfragment = new FirstFragment();
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                transaction.replace(R.id.content_frame, regcomplainfragment);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });

        btnFragmentTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction fragmenttransaction = getFragmentManager().beginTransaction();
                SecondFragment regcomplainfragment = new SecondFragment();
                fragmenttransaction.add(R.id.content_frame, regcomplainfragment);
                fragmenttransaction.commit();
            }
        });
    }

Here is FirstFragment.

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

    public class FirstFragment extends Fragment {
        public FirstFragment() {
            // Required empty public constructor
        }
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.first_fragment, container, false);
    
            return view;
        }
    }

Similarly SecondFragment too created. But when button is clicked, it is not redirecting to FirstFragment.

Here is activity_main.xml:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 1"
        android:id="@+id/fragment_button_2"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 2"
        android:id="@+id/fragment_button_1"
        android:layout_above="@+id/fragment_button_2"
        android:layout_alignLeft="@+id/fragment_button_2"
        android:layout_alignStart="@+id/fragment_button_2"
        android:layout_marginBottom="33dp" />
    </LinearLayout>
</FrameLayout>

1 answer

  • answered 2022-05-04 12:54 S_i_l_e_n_t C_o_d_e_r

    You can replace the fragment using FragmentTransaction on button click. Something like this:

    Fragment someFragment = new SomeFragment(); 
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, someFragment ); // give your fragment container id in first parameter
        transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
        transaction.commit(); 
    

    code:

    package com.rupomkhondaker.sonalibank;
    
    import android.app.Fragment;
    import android.app.FragmentTransaction;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    
    public class HomeFragment extends Fragment implements View.OnClickListener {
    
        public HomeFragment() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    
            Button aboutBtn = (Button) rootView.findViewById(R.id.aboutusButton);
            Button phonebookBtn = (Button) rootView.findViewById(R.id.phbookButton);
    
            aboutBtn.setOnClickListener(this);
            phonebookBtn.setOnClickListener(this);
    
    
            return rootView;
        }
    
        @Override
        public void onClick(View view) {
            Fragment fragment = null;
            switch (view.getId()) {
                case R.id.aboutusButton:
                    fragment = new AboutFragment();
                    replaceFragment(fragment);
                    break;
    
                case R.id.phbookButton:
                    fragment = new PhoneBookFragment();
                    replaceFragment(fragment);
                    break;
            }
        }
    
        public void replaceFragment(Fragment someFragment) {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.frame_container, someFragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    
    
    }
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum