Get the current fragment object
Now at some point of time I need to identify which object is currently there
Call findFragmentById()
on FragmentManager
and determine which fragment is in your R.id.frameTitle
container.
If you are using the androidx
edition of Fragment
— as you should in modern apps — , use getSupportFragmentManager()
on your FragmentActivity
/AppCompatActivity
instead of getFragmentManager()
I think you can use onAttachFragment event may be useful to catch which fragment is active.
@Override
public void onAttachFragment(Fragment fragment) {
// TODO Auto-generated method stub
super.onAttachFragment(fragment);
Toast.makeText(getApplicationContext(), String.valueOf(fragment.getId()), Toast.LENGTH_SHORT).show();
}
Try this,
Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.fragment_container);
this will give u the current fragment, then you may compare it to the fragment class and do your stuffs.
if (currentFragment instanceof NameOfYourFragmentClass) {
Log.v(TAG, "find the current fragment");
}