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()
How to get current fragment from MainActivity
You can get your current fragment like this:
if (getFragmentManager().getBackStackEntryCount() > 1) {
Fragment f = getFragmentManager().findFragmentById(R.id.content_frame);
if (f instanceof BlankFragment) {
// Do something
}
}
How to get current fragment in android using ById or ByTag
Your Solution is here
1. First of you replace the fragment replace line with this line.
fragTrans.replace(android.R.id.content, f1,f1.getClass.getName());
Here f1.getClass,getName()
is key of current Fragment. it Gives you name of fragment which is replace or current replace fragment.
FragmentManager manager = getSupportFragmentManager();
Fragment1 fragment1 = (Fragment1) manager.findFragmentByTag(Fragment1.class.getName());
Fragment2 fragment2 = (Fragment2) manager.findFragmentByTag(Fragment2.class.getName());
Fragment3 fragment3 = (Fragment3) manager.findFragmentByTag(Fragment3.class.getName());
if (fragment1 != null) {
Log.e(TAG, "Current fragment is Fragment1");
}else if(fragment2 != null) {
Log.e(TAG, "Current fragment is Fragment2");
}else if(fragment3 != null) {
Log.e(TAG, "Current fragment is Fragment3");
} else {
Log.e(TAG, "Fragment 1-2-3 is null");
}
If you do something on first time onBackPress Follow the code below,
@Override
public void onBackPressed() {
if (isBackPress) { //Default is false
super.onBackPressed();
} else {
// On First time click do something
isBackPress=true;
}
}
Android find current fragment
Try This it may be help to you
Fragment currentFragment = getFragmentManager().findFragmentById(R.id.where);
if (currentFragment instanceof F1) {
//do your stuff here
}
How I can retrieve current fragment in NavHostFragment?
Reference to the displayed fragment (AndroidX):
java
public Fragment getForegroundFragment(){
Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
return navHostFragment == null ? null : navHostFragment.getChildFragmentManager().getFragments().get(0);
}
kotlin
val navHostFragment: Fragment? =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
navHostFragment?.childFragmentManager?.fragments?.get(0)
Here nav_host_fragment
is an ID
of the fragment
tag in your activity_main.xml
with android:name="androidx.navigation.fragment.NavHostFragment"
Android Navigation Architecture Component - Get current visible fragment
I managed to discover a way for now and it is as follows:
NavHostFragment navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host);
navHostFragment.getChildFragmentManager().getFragments().get(0);
In case of course you know it is the first fragment. I am still investigating a way without this. I agree it is not the best way but that should be something for now.
How to get the currently displaying fragment in Nav Controller
I know there are already 2 answers on mine here but still have 1 more solution so I am writing it here. This is the code:
NavController navController = Navigation.findNavController(this, R.id.fragment);
int id=navController.getCurrentDestination().getId();
if(id==R.id.startGameFragment ){ // change the fragment id
selectedPosition(0);
}else if(id==R.id.gameFragment ){ // change the fragment id
selectedPosition(1);
}else if(id==R.id.endGameFragment ){ // change the fragment id
selectedPosition(2);
}
Related Topics
Imageview of Png Doesn't Appear (Maybe It's Too Big)
How to Make Push Notification With Specific Sound
Link Should Be Open in Same Web View in Android
Android Scrollview Not Scrolling Down After Keyboard Opens
Decrease Image Size Without Losing Its Quality in Android
Videoview to Match Parent Height and Keep Aspect Ratio
Passing an Arraylist of Objects to the New Activity
Eclipse Compiles Successfully But Still Gives Semantic Errors
How to Set Spinner Default by Its Value Instead of Position
Get City Name and Postal Code from Google Place API on Android
Send Post Request With Json Data Using Volley
How to Disable Recyclerview Items from Clicking
Alarm Manager Won't Work When the App Is Killed in the Background and Device Is Locked
Where Is Adb.Exe in Windows 10 Located