Shariful Ronie
Shariful Ronie

Reputation: 57

one Fragment to another Fragment on Button click

I have learned how to change the fragment activity from one to another (all are fragment activities) on button click but now I am having issue with multiple button on same fragment. only first button id works. I have more than one button and each button has different fragment activity. need help

package com.test.fragmentation;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class List extends Fragment {


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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_list, container, false);


        Button ID = (Button) rootView.findViewById(R.id.btnHello);
        ID.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    HelloFragment NAME = new HelloFragment();
                    fragmentTransaction.replace(R.id.fragment_container, NAME);
                    fragmentTransaction.commit();

                }
            });
        return rootView;
    }
}

Upvotes: 3

Views: 9445

Answers (3)

sanju
sanju

Reputation: 51

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_profile, container, false); notification = (Button)v.findViewById(R.id.notification);

    notification.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentTransaction fr = getFragmentManager().beginTransaction();
            fr.replace(R.id.container(this is the main activity container layout),new NotificationFragment(this is the fragment you want to go)());
            fr.commit();
        }
    });

    return v;
}

Upvotes: 0

jakir hussain
jakir hussain

Reputation: 316

Button ID = (Button) rootView.findViewById(R.id.btnHello);
            ID.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                       FragmentName Name = new FragmentName ();//Name of fragment where you want to go. 

FragmentTransaction fragmentTransaction= 

getActivity().getSupportFragmentManager().beginTransaction();  

fragmentTransaction.replace(R.id.frame, Name);              

fragmentTransaction.commitAllowingStateLoss();

                    }
                });

Upvotes: 0

Navjot.jassal
Navjot.jassal

Reputation: 779

Change the fragment name with that in which want to move

Button ID = (Button) rootView.findViewById(R.id.btnHello);
            ID.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        FragmentManager fragmentManager = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        HelloFragment NAME = new HelloFragment();
                        fragmentTransaction.replace(R.id.fragment_container, NAME);
                        fragmentTransaction.commit();

                    }
                });

Change Fragment name on that button click:-

ABC NAME = new ABC ();

Upvotes: 2

Related Questions