S
S
Sergey Shilovsky2018-11-06 21:51:07
Android
Sergey Shilovsky, 2018-11-06 21:51:07

Where to change the fragment?

I have a main activity with a FrameLayout in which I place a fragment and a bottomNavigation to navigate between these fragments. The fragments themselves have buttons that open new fragments instead of them, this is done as follows:

FragmentManager manager = getActivity().getSupportFragmentManager();
getActivity().findViewById(R.id.button).setOnClickListener(v -> manager.beginTransaction().replace(R.id.frame_layout, new Fragment).commit());

Is this the right approach to change fragments? It is assumed that nested fragments will also be able to open new fragments. Maybe I should replace not the contents of the container, but myself as a fragment?
And a side question, is it possible to save/load a stack of transactions? I want to make a separate story for each bottomNavigation item.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor, 2018-11-07
@First_Spectr

Your logic uses an approach where you assign different handlers to the same button in each fragment.
For the purposes of changing fragments, there is a good guide from Google itself that says to implement Callbacks. This makes your application easier to maintain and scalable.
This is the essence of the question that you asked (mentally replace "operation" with Activity).
I highly recommend reading the entire article. And in general with the general methods of developing applications from Google, because many developers follow them as an unspoken standard, and if you have to maintain / change someone else's project in the future, it is likely that you will already be familiar with the techniques used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question