-2

I want to display a fragment to the user instead of the current fragment that is shown.

I have been messing around with Fragment Managers and Fragment Transactions for days, I don't understand it. Please, somebody, help me in simple English, how to do it. I've been sent code examples and everything, and it's extremely frustrating. Please I request you, I just want fragmentRegister to go away, and fragment Login to show up onClick of textViewLogin.

Here's the code

RegisterFragment.java

public class RegisterFragment extends Fragment implements View.OnClickListener {

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

 @Override
    public void onClick(View view) {
        if(view == textViewLogin)
            return inflater.inflate(R.layout.fragment_login, container, false);
    }

}

I don't know if it's needed but heres the Main Activity.java

public class MainActivity extends AppCompatActivity {


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


        final DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);

        findViewById(R.id.imageMenu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });

        NavigationView navigationView = findViewById(R.id.navigationView);
        navigationView.setItemIconTintList(null);

        NavController navController = Navigation.findNavController(this, R.id.navHostFragment);
        NavigationUI.setupWithNavController(navigationView, navController);


    }
}
usr30911
  • 2,731
  • 7
  • 26
  • 56
John B
  • 43
  • 1
  • 7
  • 3
    I suggest you just read the official documentation on Fragments [here](https://developer.android.com/guide/components/fragments). It explains how to implement it in great detail – Ivo Feb 07 '20 at 14:29
  • 1
    What have you tried and in what ways is it not working? How does your problem differ from any already answered questions like [this](https://stackoverflow.com/a/13221546/2550406)? – lucidbrot Feb 07 '20 at 14:32
  • There might be people around who know this by heart. I don't, so I can only help you with googling and trying myself. But my first step would be to find some tutorial on it. When you say "it won't let me do it", it doesn't help with figuring out the problem - how does it fail? without any error message? – lucidbrot Feb 07 '20 at 14:35
  • Another related question: https://stackoverflow.com/a/8163610/2550406 – lucidbrot Feb 07 '20 at 14:37

3 Answers3

4

With androidx you can use navigation. It is just like storyboard in IOS. You make a folder navigation under resnavigation folder under res/ Inside you make a navigation graph where you put and connect the fragments.

In the activity layout you put the fragment layout:

<fragment
    android:id="@+id/frame_layout"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:defaultNavHost="true"
    app:navGraph="@navigation/before_offer_navigation"
    android:layout_width="match_parent"
    android:layout_height="0dp"

    app:layout_constraintBottom_toTopOf="@+id/bottom_navigation_menu"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />

The first item in the navigation graph will be displayed by default. Then in the fragments onViewCreated you initialise navController

private lateinit var navController: NavController
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    navController = Navigation.findNavController(view)

}

after that navigation between fragments is as easy as

navController.navigate(R.id.action_fragment1_to_fragment2)

The id you can find in the navGraph-> text-> action

<fragment
    android:id="@+id/fragment1"
    android:name="com.example.mark7.Fragment1"
    android:label="Fragment1">
    <action
        android:id="@+id/action_fragment1_to_fragment2"
        app:destination="@id/fragment2" />
</fragment>
PleaseHelp
  • 53
  • 1
  • 7
  • Hi, thanks for answering. What class will initialise navController? It doesn't seem to work well inside a fragment.java or the MainActivity.java? I already have a navigation thing set up in my project as I'm using a navigation drawer. – John B Feb 07 '20 at 14:55
  • You initialise it in the fragment class – PleaseHelp Feb 07 '20 at 15:08
  • I've tried intitalizing it there but its basically all red lines. I think your code is in koitlin whereas mine is in java? – John B Feb 07 '20 at 15:10
  • Kotlin to Java difference is little. Try NavController navController = newNavController(); navController.findNavController(view); – PleaseHelp Feb 07 '20 at 15:13
  • Follow this answer its standard way of doing – Afzal Khan Feb 08 '20 at 05:16
2

Take an Framelayout into your MainActivity and default set visibility gone.

when user click on your textViewLogin then visible that framelayout and set your loginfrgament into it like below code

    @Override
        public void onClick(View view) {
            if(view == textViewLogin){
                  containerView.setVisibility(VISIBLE);
                  LoginFragment fragment=new LoginFragment()
                  FragmentManager fragmentManager = getSupportFragmentManager();
                  fragmentManager.beginTransaction().add(R.id.containerView,fragment).addToBackStack("login").commit();
                  //here R.id.containerView is id of your frameLayout.
        }
    }
niceumang
  • 1,347
  • 1
  • 8
  • 21
  • I've tried this way but it always says "cannot resolve method getSupportFragmentManager();" – John B Feb 07 '20 at 14:35
  • will you try with FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); line in your code ?? just add getActivity() – niceumang Feb 07 '20 at 14:39
  • Thanks a million for your help. The getActivity() solved that, but I'm not sure what to put in for containerView? I'm using a navigation view with a navigation drawer but I'm not sure that is relevant. – John B Feb 07 '20 at 14:44
  • i will help you if you share your project with me if you look like it is okay for your data privacy. i assure you for your project privacy – niceumang Feb 07 '20 at 14:47
  • Thanks. How will I share my project with you? – John B Feb 07 '20 at 14:56
  • mail id is prefect way for it – niceumang Feb 07 '20 at 14:56
  • will i provide you my mail you can share it with weTransfer web? – niceumang Feb 07 '20 at 14:57
  • @JohnB share your gmail id i will greet you there thenafter you can share your code with me – niceumang Feb 10 '20 at 06:22
0

Add action inside nav_graph for fragment as

<fragment
    android:id="@+id/splashFragment"
    android:name="com.tech.SplashFragment"
    android:label="fragment_splash"
    tools:layout="@layout/fragment_splash" >
    <action
        android:id="@+id/action_splashFragment_to_loginFragment"
        app:destination="@id/loginFragment" />
</fragment>

and use this line from your fragment

            findNavController().navigate(R.id.action_splashFragment_to_loginFragment)
Gayatri Patel
  • 816
  • 8
  • 8