android - Confused with remove Fragment -


i have activity button , framelayout in layout. when click button add fragment activity's view. if add fragment stack addtobackstack() when click button dissapears. want achieve same functionality clicking again button.

my code :

     button.setonclicklistener(new view.onclicklistener() {          public void onclick(view v) {               addremovefragment frag_a = new addremovefragment();               fragmentmanager fm1 = getsupportfragmentmanager();              fragmenttransaction transaction = fm1.begintransaction();          if ( state == 0 ) {                  log.i(tag, "inside if");                  state=1;                  transaction.add(r.id.fragment_container_1, frag_a);                  transaction.addtobackstack(null);                  transaction.commit();               } else {                  state=0;                  log.i(tag, "inside else");                  //transaction.replace(r.id.fragment_container_1, frag_a);                      transaction.remove(frag_a);                  transaction.commit();              }           }      }); 

both remove() , hide() nothing. reference don't understand more specific. says removes fragment container. isn't want?remove fragment framelayout?

edit: hope has nothing support library. saw having problems that. here

xml :

<?xml version="1.0" encoding="utf-8"?> 

<button     android:id="@+id/button_frag_1"     android:layout_width="124dp"     android:layout_height="wrap_content"     android:text="@string/button_text_1" />  <framelayout     android:id = "@+id/fragment_container_1"     android:layout_width="80dp"     android:layout_height="wrap_content"     android:layout_alignbottom="@+id/button_frag_1"     android:layout_alignparentright="true"     android:layout_alignparenttop="true"     android:layout_torightof="@+id/button_frag_1" > </framelayout> 

edit 2: changed code inside else statement transaction.replace(r.id.fragment_container_1, frag_a); transaction.remove(frag_a); still got same functionality.

for fragments, first of need remember 1 thing: if added fragment in xml layout, can't removed, can shown using .show() method , hidden using .hide() method. if on other hand create instance of fragment in code should add using .add() method or remove using .remove() method.

as regard question, dont think need add fragment stack if want remove fragment using button (unless want keep functionality of removing using 'back' button).

in addition don't think need use replace, documentation of replace:

replace existing fragment added container. same calling remove(fragment) added fragments added same containerviewid , add(int, fragment, string) same arguments given here.

it means replaces content of container new fragment, remove fragment , add again.

you should .add() fragment when want show , .remove() when dont.

update:

following second question, when can add fragment in xml mean can write this:

<fragment     xmlns:map="http://schemas.android.com/apk/res-auto"     android:id="@+id/listfragment"     android:name="com.eadesign.yamba.timelinelistfragment"     android:layout_width="match_parent"     android:layout_height="match_parent" /> 

in xml layout file inside framelayout fragment container, in case cant remove fragment can hide it.

and clarify have provide kind of layout container of fragment/fragments.

as opposite can doing in code:

addremovefragment frag_a = new addremovefragment(); transaction.add(r.id.fragment_container_1, frag_a); transaction.addtobackstack(null); transaction.commit(); 

in case fragment can removed.

update2:

try take line: addremovefragment frag_a = new addremovefragment(); outside of setonclicklistener method scope. think problem fact creating new instance of fragment on every click of button. in fact move line fragmentmanager fm1 = getsupportfragmentmanager(); out side there no need instance of supportfragmentmanager on each click of button. should once.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -