java - Fragement reference from another activity returns null -


i start details fragment in new activity when in portrait mode so....

fdetail=(historyofragdetail) manager.findfragmentbyid(r.id.fragment2);         if(fdetail!=null&& fdetail.isvisible())         {             fdetail.changedata(st);          }else{ // portrait               intent intent = new intent(this, historyoactfrag.class);             intent.putextra("index", st);               startactivity(intent);         }  

while in portrait mode load fragment so...

public class historyoactfrag extends sherlockfragmentactivity...  @override protected void oncreate(bundle savedinstancestate) {  // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.history_actifrag_layout);  intent intent = getintent(); string myindex = intent.getstringextra("index");      historyofragdetail f2 = (historyofragdetail)getsupportfragmentmanager().findfragmentbyid(r.id.fragment2);     f2.changedata(myindex);    } 

the layout follows...

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent" >          <fragment         android:id="@+id/fragment2"         android:name="com.myfragtesting.oin.historyofragdetail"         android:layout_width="match_parent"         android:layout_height="match_parent" />  </relativelayout> 

the problem...

every time try reference fragment in activity came npe. code additions fail below.

public class historyoactfrag extends sherlockfragmentactivity...  @override protected void oncreate(bundle savedinstancestate) {  // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.history_actifrag_layout);  intent intent = getintent(); string myindex = intent.getstringextra("index");  historyofragdetail f2 = (historyofragdetail)getsupportfragmentmanager().findfragmentbyid(r.id.fragment2); f2.changedata(myindex);   /// ..................new step here ....     historyofragmaster f1 = (historyofragmaster )getsupportfragmentmanager().findfragmentbyid(r.id.fragment1);     log.d("mylog","......f1 = "+f1");  ////   = null ???? why can't ?    } 

when in landscape keep fragments in 1 activity , reference work fine.

i realize fragment1 in activity came can not reference when fragments in 2 separate activities ? better yet correct way reference f1 ?

-thanks help.

while activity running can add fragment layout using these 4 steps.

  1. get refrence fragment manager
  2. begin fragment transaction
  3. add fragment
  4. commit fragment transaction

    fragmentmanager mfragmentmanager = getfragmentmanager(); fragmenttransaction fragmenttransaction = mfragmentmanager         .begintransaction(); fragmenttransaction.add(r.id.title_fragment_container,         new titlesfragment()); fragmenttransaction.commit(); 

make sure using commit.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -