android - How to replace a fragment and change TextView in the new one in the same procedure? -
hello you!
i not native english speaker, hope understand me anyway.
in application have activity relativelayout wich use fragment-container.
<framelayout 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" > <relativelayout android:id="@+id/fragmentcontainer" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" /> </framelayout>
at time have 2 different fragments, 1 shown @ startup , die other 1 called click on textview in first one. both fragments have own class , first 1 has interface main activity perform replacement of fragment after onclick.
and there problem....
public void oncallforagb() { getfragmentmanager().begintransaction() .replace(r.id.fragmentcontainer, new loginreaderfragment(), "agbreader") .addtobackstack(null) .commit(); loginreaderfragment fragment = (loginreaderfragment) getfragmentmanager() .findfragmentbyid(r.id.fragmentcontainer); if (fragment != null && fragment.isinlayout()) { fragment.settext("gruß die welt!"); } else { toast.maketext(mcontext, "fail", toast.length_long).show(); } }
just explaining: container holds fragment named loginloginframent
. user of app can login or click on link (textview) show agb (dont know how translate, maybe "therms use , law dependencies" ??). via interface
oncallforagbwill executed , replace fragment fragment called
loginreaderfragment` wich fragment headline (textview) , textfield (textview) show filecontents. function settext(string) should set text after fragment created.
but after fragmenttransaction.commit()
seems change not executed immediately. seems executed when leaving whole procedure. can't access new fragments views (to change text) without complete perfomed replace commit.
so here question: how can force fragmenttransaction executed after commit() ? or there workaround, can change headline , text of new fragment right after changing first second fragment?
here errorreport (logcat) why have assumption
05-29 20:59:18.748: e/androidruntime(14334): java.lang.classcastexception: de.example.myapp.fragments.loginloginfragment cannot cast de.example.myapp.fragments.loginreaderfragment
i think loginreaderfragment fragment = ....
create error because old fragment (loginloginfragmet) still active.
hope can me. greets !
do not try set fragments ui data activity after creating it. instead, give fragment information, should shown after fragment visible. best way use getinstance() pattern. see here.
you need call settext function onviewcreated() in fragment itself. grants data set, after view availible.
Comments
Post a Comment