Trzy Gracje
Trzy Gracje

Reputation: 3069

getFragmentManager().getFragmentById(R.id.fragment) returns null

I have made a simple app which uses an action bar and I want to detect which Fragment is loaded. Unfortunately the following line always returns null:

getFragmentManager().getFragmentById(R.id.fragment) 

To load new Fragments, I am using:

getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();

Upvotes: 0

Views: 2445

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81539

Because the FragmentTransaction sets the fragment's ID to be R.id.content_frame which is the container. So getFragmentManager().getFragmentById(R.id.content_frame) will give you the Fragment.

Also, it is recommended to use the support library's Fragments and appcompat-v7 (getSupportFragmentManager() and android.support.v4.Fragment, extending from appcompat's ActionBarActivity) because they are more maintained.

Upvotes: 6

Related Questions