Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Check if a fragment is visible [duplicate]

Tags:

android

I have a FragmentActivity, some fragments and a service that is running in background. It is possible to check from the service if a specific fragment is visible?

like image 522
user2538848 Avatar asked Jul 01 '13 12:07

user2538848


People also ask

How do you know if a fragment is visible?

In ViewPager2 and ViewPager from version androidx. fragment:fragment:1.1. 0 you can just use onPause and onResume callbacks to determine which fragment is currently visible for the user. onResume callback is called when fragment became visible and onPause when it stops to be visible.

How do I get NavController in fragment?

To retrieve the NavController for a fragment, activity, or view, use one of the following methods: Kotlin: Fragment. findNavController()

What is findFragmentByTag?

This method allows you to retrieve the instance of a previously added fragment with the given tag regardless of the container it was added to.

How do you know if a fragment is destroyed?

Since all fragments are destroyed if the activity is destroyed, a simple answer could be calling getActivity(). isDestroyed() returning true if the activity is destroyed, therefore the fragment is destroyed.


2 Answers

check if(YourFragment.this.isVisible())

like image 152
Mr Roshan Pawar Avatar answered Sep 22 '22 12:09

Mr Roshan Pawar


public boolean isFragmentUIActive() {     return isAdded() && !isDetached() && !isRemoving(); } 

Does the trick.

like image 40
meredrica Avatar answered Sep 24 '22 12:09

meredrica