Issue
I launch my dialog fragment using
FragmentTransaction ft =
getFragmentManager().beginTransaction();
MyDialogFragment dialog = new MyDialogFragment()
dialog.show(ft, "dialog");
then to get a handle on it I do
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
but once I get prev
, how do I check if it is showing?
Back Story
My problem is that my looping code keeps launching the dialog again and again. But if the dialog is already showing, I don't want it to launch again. This back story is just for context. The answer I seek is not: "move it out of the loop."
Solution
simply check if it's null
if(prev == null)
//There is no active fragment with tag "dialog"
else
//There is an active fragment with tag "dialog" and "prev" variable holds a reference to it.
Alternatively, you could check the activity the fragment prev
is currently associated with, however, make sure you ask that after you make sure it's not null or you'll get a NullPointerException. Like this:
if(prev == null)
//There is no active fragment with tag "dialog"
else
if(prev.getActivity() != this) //additional check
//There is a fragment with tag "dialog", but it is not active (shown) which means it was found on device's back stack.
else
//There is an active fragment with tag "dialog"
Answered By - nstosic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.