Home > other >  How can I change the fragment in current fragment?
How can I change the fragment in current fragment?

Time:12-23

I am developing a mobile application using with java in android studio.I am new to this environment.I have bottom navigation bar with 3 menu item (Home,Add article,Profile).

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@ id/home"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="Home" />
    <item
        android:id="@ id/article"
        android:icon="@drawable/ic_baseline_post_add_24"
        android:title="Article" />
    <item
        android:id="@ id/profile"
        android:icon="@drawable/ic_baseline_person_24"
        android:title="Profile" />
</menu>

and in profile fragment I have two buttons(change password,update profile informations).When I click the change password button,I want to change current fragment to changePasswordFragment.But navigation bar has to be shown at the bottom . How can I figure that?

CodePudding user response:

You have to use fragment transactions to show one fragment and remove the other in the UI like in the code below.

profileFragment = new ProfileFragment();
changePassFragement= new ChangePassFragment();
changePassBtn.setonClickListener(e->{
getSupportFragmentManager().beginTransaction().replace(R.id.profileLayout,changePassFragment).commit()
});

and to replace back useprofileFragment instead of changePassFragment and the required layout in the replace() method.

CodePudding user response:

You can use fragment transaction manager to add the new fragments.Given code.

changePassBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();   
transaction.replace(R.id.profile, new ChangePassFragment());
transaction.commit();
}
});

And again you can use the same code for return to the profile fragment by using replace method.

  • Related