user3227070 user3227070 - 4 months ago 35
Android Question

How to switch between fragments using buttons

Fragment 1:

public class homePage extends Fragment {


private OnFragmentInteractionListener mListener;
private View view;

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home_page, container, false);
Button btnLogin = (Button) view.findViewById(R.id.login);
Button btnRegister = (Button) view.findViewById(R.id.register);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginView();
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerView();
}
});
return view;
}


public static homePage newInstance() {
homePage fragment = new homePage();
Bundle args = new Bundle();
return fragment;
}


public void registerView(){}
public void loginView(){}


public interface OnFragmentInteractionListener {

}
}


Acitivty :

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
homePage homepage = new homePage();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, homepage)
.commit();
}
}




}


My fragment1 has two buttons with the id's "login" and "register". Whenever login is clicked, I want to go to my fragment_login.xml, and when register is clicked I want to go to my fragment_register.xml. Should I create these functions in my activity or in my fragment? And how should I do this? I'm fairly new to android and I'm trying to learn these basic things atm. Thanks for the help :(

Login context: "com.example.hoofdgebruiker.winkelskortrijk.login"
Register context: "com.example.hoofdgebruiker.winkelskortrijk.register"

Answer

You have to make a communication between Fragment objects via an Interface.

The Activity that make the switch between your fragments needs to implement that Interface.

So, in your case your classes must be defined as follows. This is your Interface, it helps you to tell your activity that a button has been clicked from within your Fragment:

public interface HomeClickListener {
    void onLoginClick();
    void onRegisterClick();
}

Your Activity needs to implement the above interface in order to be notified when a button has been clicked:

public class HomeActivity extends Activity implements HomeClickListener {

    void onLoginClick() {
        // Your activity code to replace your current Fragment with your Login fragment
    }

    void onRegisterClick() {
        // Your activity code to replace your current Fragment with your Register fragment
    }

}

Your fragment is hosted by an Activity, so you need to have a reference to it and notify it via an Interface:

public class LoginFragment extends Fragment {

    final HomeClickListener homeClickListener

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            homeClickListener = (homeClickListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home_page, container, false);
        Button btnLogin = (Button) view.findViewById(R.id.login);
        Button btnRegister = (Button) view.findViewById(R.id.register);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                homeClickListener.onLoginClick();
            }
        });
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                homeClickListener.onRegisterClick();
            }
        });
        return view;
    }

}

More information here.

Comments