java - Fragment backstack with children fragments -


i have app has main activity drawer. every item on drawer sends me fragment.

when press button fragments, want taken main activity.

the problem have child fragment in 1 of fragments. want button in child take me parent fragment.

actually need same thing different parents.

how do that?

here mainactivity:

package com.example.matancohen.sg50;  import android.content.dialoginterface; import android.os.bundle; import android.os.handler; import android.support.v4.app.fragmentmanager; import android.support.v7.app.alertdialog; import android.text.html; import android.view.gravity; import android.support.design.widget.navigationview; import android.support.v4.view.gravitycompat; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbardrawertoggle; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.support.v4.app.fragment; import android.view.menuitem; import android.widget.toast;    public class mainactivity extends appcompatactivity     implements navigationview.onnavigationitemselectedlistener {  @override protected void oncreate(bundle savedinstancestate) {      super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);     setsupportactionbar(toolbar);      drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);     actionbardrawertoggle toggle = new actionbardrawertoggle(             this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close);     drawer.adddrawerlistener(toggle);     toggle.syncstate();      navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view);     navigationview.setnavigationitemselectedlistener(this);      displayview(r.id.nav_home);    }   @override public void onbackpressed() {     drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);     if (drawer.isdraweropen(gravitycompat.start)) {         drawer.closedrawer(gravitycompat.start);     } else {         super.onbackpressed();     }  }  private void displayview(int itemid) {     fragment fragment = null;      switch (itemid) {         case r.id.nav_home:             fragment = new article();             break;         case r.id.nav_recipes:             fragment = new recipesmain();             break;         case r.id.nav_converter:             fragment = new converter();             break;         case r.id.nav_videos:             fragment = new videos();             break;         case r.id.nav_tracking:             fragment = new tracking();             break;         case r.id.nav_trainermenu:             fragment = new trainermenu();             break;         case r.id.nav_settings:             fragment = new settings();             break;         case r.id.nav_about:             showdialog();             break;      }       if (fragment != null) {         final fragment finalfragment = fragment;         new handler().postdelayed(new runnable() {             @override             public void run() {                 getsupportfragmentmanager()                         .begintransaction()                         .setcustomanimations(android.r.anim.fade_in, android.r.anim.fade_out)                         .replace(r.id.content_main, finalfragment)                         .commit();             }         }, 0);             }          drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);         drawer.closedrawer(gravitycompat.start);     }     @suppresswarnings("statementwithemptybody") @override public boolean onnavigationitemselected(menuitem item) {      displayview(item.getitemid());     return true; } } 

recipe fragment:

package com.example.matancohen.sg50;  import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.support.v4.view.gravitycompat; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imagebutton; import android.support.v4.app.fragmentactivity;   public class recipesmain extends fragment implements view.onclicklistener { @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) {     return inflater.inflate(r.layout.fragment_recipes,container,false); } @override public void onviewcreated(view view, @nullable bundle savedinstancestate) {     super.onviewcreated(view, savedinstancestate);     //you can set title toolbar here different fragments different titles     getactivity().settitle("מתכונים");     imagebutton stuffedbreast = (imagebutton)getview().findviewbyid(r.id.stuffedbreast);     stuffedbreast.setonclicklistener(this);     imagebutton breastsweetpotato = (imagebutton)getview().findviewbyid(r.id.chickensweetpotato);     breastsweetpotato.setonclicklistener(this);     imagebutton cabbage = (imagebutton)getview().findviewbyid(r.id.cabbage);     cabbage.setonclicklistener(this);     imagebutton chickenmeatballs = (imagebutton)getview().findviewbyid(r.id.chickenmeatballs);     chickenmeatballs.setonclicklistener(this);     imagebutton sushi = (imagebutton)getview().findviewbyid(r.id.sushi);     sushi.setonclicklistener(this);     imagebutton sintaballs = (imagebutton)getview().findviewbyid(r.id.sintaballs);     sintaballs.setonclicklistener(this);     imagebutton maakuda = (imagebutton)getview().findviewbyid(r.id.maakuda);     maakuda.setonclicklistener(this); }   @override public void onclick(view v) {     fragment fragment = null;       switch (v.getid()) {          case r.id.chickenmeatballs:             fragment = new recipechickenmeatballs();             break;          case r.id.chickensweetpotato:             fragment = new recipechickensweetpotato();             break;          case r.id.cabbage:             fragment = new recipecabbage();             break;          case r.id.stuffedbreast:             fragment = new recipestuffedbreast();             break;          case r.id.sushi:             fragment = new recipesushi();             break;          case r.id.sintaballs:             fragment = new recipesintaballs();             break;          case r.id.maakuda:             fragment = new recipemaakuda();             break;     }     if (fragment != null){         fragmentmanager fragmentmanager = getactivity().getsupportfragmentmanager();         fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();         fragmenttransaction.setcustomanimations(r.anim.bottomtop, r.anim.topbottom,r.anim.popbottomtop,r.anim.poptopbottom);         fragmenttransaction.replace(r.id.content_main, fragment);         fragmenttransaction.addtobackstack(null);         fragmenttransaction.commit();     } } 

my solution override onbackpressed() method in mainactivity , write code,

    @override         public void onbackpressed() {             drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);             if (drawer.isdraweropen(gravitycompat.start)) {                 drawer.closedrawer(gravitycompat.start);                 return;              }              if (getsupportfragmentmanager().findfragmentbyid(r.id.fragment_container) instanceof article) {                 ((article) getsupportfragmentmanager().findfragmentbyid(r.id.fragment_container)).handleonbackpress();                 return;             }              if (getsupportfragmentmanager().getbackstackentrycount() == 0) {                 displayview(r.id.nav_home);             } else {                 super.onbackpressed();              }         } 

and add new method in article fragment called handleonbackpress() , write custom code handling button .

example :

public void handleonbackpress() {         system.exit(0); } 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -