android - Get tag of currently displayed fragment -


i searched displayed fragment , found answers. tried findfragmentbyid.it returns null. i'm using framelayout in fragment's xml. not <fragment>. maybe that's why returns null. use id of <framelayout> in findfragmentbyid.

someone told me use tags. can't figure out how use tags set while replacing fragment check if current fragment has tag.

if particular fragment displayed selected nav drawer need exit app onbackpressed() clearing activities in stack. need see if displayed fragment particular fragment.

can please explain example. i've been doing days , can't figure out.

edit. have not posted code because want example. posted question findfragmentbyid returning null , suggested me use tags , can't figure out using tag check if it's current fragment.

i hope can you

at time of calling fragment set unique tag each fragment

      fragment fragment=new homefragment(); fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction().replace(r.id.frame_container, fragment,"home").commit(); //home tag 

in mainactivity's onbackpressed method check particular fragment visible or not

 fragment hm=getfragmentmanager().findfragmentbytag("home");      if(hm!=null)      {       if(hm.isvisible())        {          //exit application           mainactivity.this.finish();         }      } 

Comments