android - How to know how many instances of the same fragment are there in the stack, and stop a new one while adding if one already exists -
how know how many instances of same fragment there in stack, , prevent new 1 beingness added if 1 exists?
here how check whether fragment in backstack @ all
public boolean isfragmentonbackstack(string name) { // illustration isfragmentonbackstack(catfragment.class.getsimplename()) fragment f = getfragmentmanager().findfragmentbytag(name); homecoming f != null; }
here how add together them
string fragmentclassname = ((object) fragment).getclass().getsimplename(); fragmenttransaction t = getfragmentmanager().begintransaction(); t.replace(r.id.root_fragment_container, fragment, fragmentclassname); t.addtobackstack(fragmentclassname); t.commit();
now, problem is, somewhere in app, adding fragment @ time not needed. dont know when happens, want set code in fragment's onresume check whether going sec instance of same fragment , if case, stop fragment creating (not remove old 1 backstack)
you seek following
fragment fragment = getfragmentmanager().findfragmentbytag("name"); // if fragment doesn't exist yet, create 1 if (fragment == null) { transaction.add(r.id.container_id, new myfragment(), "name"); } else { // re-use old fragment transaction.replace(r.id.container_id, fragment, "name"); }
android android-fragments
No comments:
Post a Comment