Reputation: 3069
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 Fragment
s, I am using:
getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
Upvotes: 0
Views: 2445
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