java - Android won't allow the transition of more than one fragment: -


following tutorial here , stackoverflow able make button switch replace current fragment another. elaborate can replace current fragment if wish replace 1 after button app crashes. code:

mainactivity:

public class mainactivity extends activity {      public fragmenttransaction transaction = getfragmentmanager().begintransaction();     public fragment loginfragment = new loginfragment();     public fragment mainfragment = new mainfragment();     public fragment settingsfragment = new settingsfragment();      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          setcontentview(r.layout.activity_main);          if (savedinstancestate == null) {              getfragmentmanager().begintransaction().add(r.id.container, loginfragment).commit();          } else {              getfragmentmanager().begintransaction().add(r.id.container, mainfragment).commit();         }     }      public void gotomain(view v) {          transaction.replace(r.id.container, mainfragment);         transaction.addtobackstack(null);         transaction.commit();     }      public void gotosettings(view v) {          transaction.replace(r.id.container, settingsfragment);         transaction.addtobackstack(null);         transaction.commit();     }      public static class loginfragment extends fragment {          public loginfragment() {          }          @override         public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {              view rootview = inflater.inflate(r.layout.fragment_login, container, false);              return rootview;         }     }      public static class mainfragment extends fragment {          public mainfragment() {          }          @override         public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {              view rootview = inflater.inflate(r.layout.fragment_main, container, false);              return rootview;         }     }      public static class settingsfragment extends fragment {          public settingsfragment() {          }          @override         public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {              view rootview = inflater.inflate(r.layout.fragment_settings, container, false);              return rootview;         }     } } 

login fragment xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.pointlight.kingdomcraft.mainactivity$loginfragment" >      <button         android:id="@+id/button_submit"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerhorizontal="true"         android:layout_centervertical="true"         android:onclick="gotomain"         android:text="@string/action_submit" />  </relativelayout> 

main fragment xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.pointlight.kingdomcraft.mainactivity$mainfragment" >      <button         android:id="@+id/button_settings"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerhorizontal="true"         android:layout_centervertical="true"         android:onclick="gotosettings"         android:text="@string/action_settings" /> </relativelayout> 

as can see code 2 different transactions same... why android not allow switching of fragments more once?

edit: whatever reason logcat empty

you call commit() on transaction don't call begintransaction() before new replace(...) command.

edit: maybe not clear. meant:

public void gotomain(view v) {     fragmenttransaction transaction = getfragmentmanager().begintransaction();     transaction.replace(r.id.container, mainfragment);     transaction.addtobackstack(null);     transaction.commit(); } 

and on. can not reuse transaction, don't save it. create new one.


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -