Steps:-
Dependencies:-
implementation 'android.arch.navigation:navigation-fragment:1.0.0'
1. Create a new Project.2. Create a directory name "navigation" inside res folder like below
3. Right click on navigation directory and add Navigation Resource File like below :-
4.Click on MainActivity's XML file and do following :-
activity_main:-
here we have to take one fragment which will hold the fragment. And this will be also my hostFragment , 3 important thing don't forget to add:-
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/login_nav_graph"
app:defaultNavHost="true"
5.Click on navigation folder and click on login_nav_graph.xml and go to design and click on + button and click "create new designation" and create LoginFragment like below :-
6. be continue to add all classes like that and than the graph will like below:-
7.
Main Import is:-
import static androidx.navigation.Navigation.findNavController;
code that on click of various button how to write code for navigation:-
public class LoginFragment extends Fragment { Button btnMobile, btnFacebook; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_login, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); btnMobile = view.findViewById(R.id.btnMobile); btnFacebook = view.findViewById(R.id.btnFacebook); btnMobile.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { findNavController(getActivity(), R.id.btnMobile).navigate(R.id.mobileFragment, null); } }); btnFacebook.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { findNavController(getActivity(), R.id.btnFacebook).navigate(R.id.facebookFragment, null); } }); } }
No comments:
Post a Comment