Muhammad ADel Muhammad ADel - 1 month ago 4
Java Question

What is difference between findFragmentById() and creating an object of Fragment in Android?

Here is an implementation of

findFragmentById(int id)


Custom_Fragment frag = findFragmentById(id);


Here is an implementation of creating a
Fragment
object

Custom_Fragment frag = new Custom_Fragment();


What is difference between findFragmentById() and creating an object of Fragment in Android?

Answer

findFragmentById() returns an existing instance (or null, if there is no instance found with the given ID).

By using the new keyword, a new instance is created.

Comments