Junior — Middle
How is navigation within a single Activity implemented in applications?
sobes.tech AI
Answer from AI
Navigation within a single Activity in Android applications is usually implemented using fragments. Instead of launching a new Activity for each screen, you can dynamically replace or add fragments within a single Activity, allowing smooth transitions between different interfaces.
Main implementation methods:
- Using FragmentManager to add, replace, and remove fragments.
- Using the Navigation Component — a modern tool from Google that simplifies navigation and stack management of fragments.
Example of replacing a fragment:
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, NewFragment())
.addToBackStack(null) // so you can go back
.commit()
This approach helps save resources and creates a smoother user experience.