Junior — Middle
Can you explain what is meant by the concept of Fragment in Android development?
sobes.tech AI
Answer from AI
A Fragment in Android is a modular part of the user interface that represents a separate component with its own lifecycle. Fragments allow for creating flexible and adaptive interfaces, especially on devices with different screen sizes (such as tablets and phones). They are usually used within an Activity and can be added, replaced, or removed dynamically.
Example of using a fragment:
class ExampleFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_example, container, false)
}
}
Then, this fragment can be added to an Activity via FragmentManager.