android - How to navigate from fragment in one activity to fragment in another activity? -


i want add navigation toolbar. need fragment in activity specific fragment in activity.

it looks little this, every orange line means navigating new activity or fragment:

enter image description here

how move fragment b fragment otheractivity?

consider these steps:

from activity 1 holding fragment , want directly load fragment b in activity 2.

now, thinking first, press button in fragment a, can directly go activity b.

then means, can load fragment b arrive in activity 2.

since dealing navigation (i believe mean upnavigation?), can override following:

but watch clearly, because if need load exact fragment in activity 2, need know somehow:

@override public boolean onoptionsitemselected(menuitem item) {     switch (item.getitemid()) {          case android.r.id.home:             intent intent = new intent(this, activity2.class);             intent.putextra("frag", "fragmentb");             startactivity(intent);             return true;         default:             return super.onoptionsitemselected(item);     }  } 

as can see, when click arrow on toolbar, pass value through our intent identity fragment want load.

next, in activity2, intent extra , switch or if statement:

@override public void onresume(){   super.onresume();    intent intent = getintent();    string frag = intent.getextras().getstring("frag");    switch(frag){      case "fragmentb":        //here can set fragment b activity usual;         fragmentmanager.begintransaction().replace(r.id.container_body, new fragmentb()).commit();        break;   } } 

from here, should have fragment b showing in activity 2.

now can handle same thing while inside activity 2 decide go when user clicks home arrow!

i hope helps idea.

note: thought interface approach , realized not necessary since can done approach!


Comments