java - SplashScreen Activity onClick to open MainActivity and replace fragment in that Activity's fragment container -


hi struggling one, hence first post after day of searching solution no luck.

i have splash screen has imageview onclicklistener when clicked opens mainactivity fine:

splashactivity.java

      bookmain.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             intent intent = new intent(splashactivity.this, mainactivity.class);             startactivity(intent);             } 

however i'm trying achieve is, when button clicked, opens mainactivity, replace's fragment_container in mainactivity have fragment (this how bottombar navigation works).

this code in mainactivity handles replacing of fragments when navigation buttons clicked , want splash screen's click open "bookfragment" rather go straight "roomsfragment":

mainactivity.java

public class mainactivity extends appcompatactivity {      //standard on create open load initial layout @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);   //this creates blank container each fragment create go in     if (findviewbyid(r.id.fragment_container) != null) {         // however, if we're being restored previous state,         // don't need , should return or else         // end overlapping fragments.         if (savedinstancestate != null) {             return;         }         // create new fragment placed in activity layout         roomsfragment firstfragment = new roomsfragment();         // add fragment 'fragment_container' framelayout         getsupportfragmentmanager().begintransaction()                 .add(r.id.fragment_container, firstfragment).commit();   //this creates nav bar , tells fragment put in container when tab selected         bottombar bottombar = (bottombar) findviewbyid(r.id.bottombar);         bottombar.setontabselectlistener(new ontabselectlistener() {             @override             public void ontabselected(@idres int tabid) {                 fragmenttransaction transaction = getsupportfragmentmanager().begintransaction();                  //tab1 - default tab                 if (tabid == r.id.tab_rooms) {                      roomsfragment newfragment = new roomsfragment();                     transaction.replace(r.id.fragment_container, newfragment);                     transaction.addtobackstack(null);                     transaction.commit();                      //tab2                 } else if (tabid == r.id.tab_shisha) {                      // bit below changes fragment in container                     shishafragment newfragment = new shishafragment();                     transaction.replace(r.id.fragment_container, newfragment);                     transaction.addtobackstack(null);                     transaction.commit();                      //tab3                 } else if (tabid == r.id.tab_cocktails) {                      // bit below changes fragment in container                     cocktailsfragment newfragment = new cocktailsfragment();                     transaction.replace(r.id.fragment_container, newfragment);                     transaction.addtobackstack(null);                     transaction.commit();                      //tab4                 } else if (tabid == r.id.tab_food) {                      // bit below changes fragment in container                     foodfragment newfragment = new foodfragment();                     transaction.replace(r.id.fragment_container, newfragment);                     transaction.addtobackstack(null);                     transaction.commit();                      //tab5                 } else if (tabid == r.id.tab_book) {                      // bit below changes fragment in container                     bookfragment newfragment = new bookfragment();                     transaction.replace(r.id.fragment_container, newfragment);                     transaction.addtobackstack(null);                     transaction.commit();                 }               }           }); 

corrected working splashactivity.java:

in oncreate method:

//onclick listener enter button , book button on splash page       button enterbutton = (button) findviewbyid(r.id.enter_button);     enterbutton.setonclicklistener(splashactivity.this);      button bookbutton = (button) findviewbyid(r.id.book_button);     bookbutton.setonclicklistener(splashactivity.this);  }  public void onclick(view v) {     intent intent = new intent(splashactivity.this, mainactivity.class);     switch (v.getid()) {         case r.id.enter_button:              startactivity(intent);             break;         case r.id.book_button:              intent.putextra("source","onclick");             startactivity(intent);             break;         default:             break;     } } 

corrected working mainactivity.java

in oncreate method (enter position of tab of bottombar relevant you):

        intent intent = getintent();         string source = intent.getstringextra("source");         if (source != null && source.equals("onclick")) {             bottombar.selecttabatposition(4);         } 

good luck!

because use this:

getsupportfragmentmanager().begintransaction()             .add(r.id.fragment_container, firstfragment).commit(); 

it's show firstfragment default

you can try this:

in spalsh activity

intent intent = new intent(splashactivity.this, mainactivity.class); startactivity(intent); 

add params intent

intent intent = new intent(splashactivity.this, mainactivity.class); intent.putextra("source","onclick");   //add line startactivity(intent); 

in main activity

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      ```your code      intent intent = getintent();     string source = intent.getstringextra("source");     if (source.equals("onclick")) {         bottombar.ontabselected(r.id.tab_book);     } } 

when main activity incept params source,and source value set, it's run code in if


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -