Home » Android » android – Linking Fragments With Buttons-Exceptionshub

android – Linking Fragments With Buttons-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

Can someone please help me?
i just want to add 2 Buttons in a activity and each button goes to a different fragment
for example:
Button 1 goes to Login_Fragment
Button 2 goes to SignUp_Fragment
those fragments have their own xml files
the code below is FirstPage and it’s xml is first_page
so the below code will replace the first_page xml with the fragments xml files
how can i prevent that?

package com.example.logindesign;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

public class First_Page extends AppCompatActivity {
    Button createAccount, login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_page);



        createAccount = findViewById(R.id.createAccount);
        createAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = getSupportFragmentManager();
                SignUp_Fragment f2 = new SignUp_Fragment();
                fm.beginTransaction().replace(R.id.mParent, f2).commit();
            }
        });


        login = findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = getSupportFragmentManager();
                Login_Fragment f2 = new Login_Fragment();
                fm.beginTransaction().add(R.id.mParent, f2).commit();
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/Blue"
    android:id="@+id/mParent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/stay_connected_with_friends"
        android:textSize="28sp"
        android:textColor="@color/white"
        android:fontFamily="@font/nunito_bold"
        android:layout_marginTop="230dp"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="200dp"
        >

    </TextView>


    <Button
        android:id="@+id/createAccount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@color/Blue"
        android:fontFamily="@font/nunito_bold"
        android:text="@string/sign_up"
        android:textColor="@color/white"
        android:textSize="25sp">

    </Button>

    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="200dp"
        android:layout_marginBottom="172dp"
        android:background="@color/Blue"
        android:fontFamily="@font/nunito_bold"
        android:text="@string/login"
        android:textColor="@color/white"
        android:textSize="25sp">

    </Button>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="175dp"
        android:layout_height="141dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="114dp"
        android:layout_marginBottom="136dp"
        android:contentDescription="@string/logo"
        app:srcCompat="@drawable/logo" />





</RelativeLayout>
How to&Answers:

I use FragmentManager
https://developer.android.com/reference/androidx/fragment/app/FragmentManager?hl=en
and use tags to determine how the navigation flows

class MainMenuFragment : Fragment() {
    private lateinit var supportFragmentManager: FragmentManager
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        viewFragment = inflater.inflate(R.layout.main_menu_fragment, container, false)
        supportFragmentManager = activity!!.supportFragmentManager

        val buttonDocumentList: Button = viewFragment.findViewById(R.id.btn_menu_top_left)
        buttonDocumentList.setOnClickListener {
            replaceFragment(
                DocumentListFragment.newInstance(), "${FragmentTypes.DOCUMENT_LIST}"
            )
        }
    private fun replaceFragment(fragment: Fragment, tag: String) {
        supportFragmentManager.beginTransaction()
            .replace(R.id.main_activity_container, fragment, tag)
            .commitNow()
    }

Answer:

use code like this,
click on button A,

 Fragment A = new FragmentA();

click on button B,

Fragment B = new FragmentB();

and make a function for both load Fragments,

public void loadFragment(Fragment f)
 {
    FragmentManager fm = getSupportFragmentManager();
    fm.beginTransaction().replace(R.id.main_contenier,f).commit();
 }