Muhammad ADel
Muhammad ADel

Reputation: 67

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

Here is an implementation of findFragmentById()

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?

Upvotes: 2

Views: 302

Answers (2)

earthw0rmjim
earthw0rmjim

Reputation: 19417

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.

Upvotes: 3

Alex Shutov
Alex Shutov

Reputation: 3282

You should create fragment by static factory method and pass arguments in Bundle. As stated above, .findFragmentById(id) finds fragment, placed in layout container with given Id. there is another method .findFragmentByTag(String tag), where string is ay user- defined string. The latter is more handy.

Upvotes: 0

Related Questions