android - First Fragment onBackPressed() -


so.. have got multiple fragments, first 1 user gets see after login considered 'welcomefragment' , rest not matter.

i want pop dialog logout procedure when user looking @ welcomefragment. how can achieve this? tried following:

@override     public void onbackpressed() {         drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);         if (drawer.isdraweropen(gravitycompat.start)) {             drawer.closedrawer(gravitycompat.start);         } else {             super.onbackpressed();             if(getfragmentmanager().getbackstackentrycount() == 0) {                 new alertdialog.builder(menuactivity.this)                         .settitle("logout")                         .setmessage("sure want logout?")                         .setpositivebutton(r.string.yes, new dialoginterface.onclicklistener() {                             public void onclick(dialoginterface dialog, int which) {                                 tool.logout();                             }                         })                         .setnegativebutton(r.string.no, new dialoginterface.onclicklistener() {                             public void onclick(dialoginterface dialog, int which) {                              }                         })                         .seticon(r.mipmap.change)                         .show();               }             else {                 getfragmentmanager().popbackstack();             }           }     } 

(the logout method)

public void logout() {     intent mainintent = new intent(context, mainactivity.class);     mainintent.addflags(intent.flag_activity_new_task);     mainintent.putextra("logout", "logout");     context.startactivity(mainintent); } 

fragment fr = getfragmentmanager().findfragmentbytag("welcomefragmenttag"); if (null != fr && fr.isadded()) {     // show dialog } else {     // not show dialog } 

this assumes, have added welcomefragment providing welcomefragmenttag transaction tag:

getsupportfragmentmanager().begintransaction()         .add(r.id.content_frame,  new welcomefragment(), "welcomefragmenttag")         .commit(); 

Comments