java - Load a fragment once and show it on more activities -


i have android application, has lot of articles. if open article new activity created. there part in activity same, send request server data. thinking make fragment load data once (probably in mainactivity) , show activities. possible , how do it?

i tried creating fragment, still loads data every time new activity created. here xml code.

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent" >      <textview         android:id="@+id/textview"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:background="#000000" />      <!-- there more layout, don't think needed -->  </relativelayout> 

and java

private class getmessage extends asynctask<void , void, void>{  @override protected void doinbackground(void... params) {      defaulthttpclient  httpclient = new defaulthttpclient();     httpget httppost = new httpget("http:///www.example.com");     httpresponse response;      try {                    response = httpclient.execute(httppost);                 httpentity ht = response.getentity();            bufferedhttpentity buf = new bufferedhttpentity(ht);             inputstream = buf.getcontent();           bufferedreader r = new bufferedreader(new inputstreamreader(is));         total = new stringbuilder();         string line;         while ((line = r.readline()) != null) {             total.append(line + "\n");         }               }      catch (clientprotocolexception e) {}      catch (ioexception e) {}      return null;  }  @override protected void onpostexecute(void args){     try{         textview.settext(total.tostring());     }     catch(exception e){} }  } 

and code add fragment

fragmentmanager fragman = getsupportfragmentmanager(); android.support.v4.app.fragmenttransaction fragtransaction = fragman.begintransaction(); fragment fragment = new fragment(); fragtransaction.add(r.id.fragment_replace, fragment); fragtransaction.commit(); 

fragments belong 1 activity. after commit() transaction, fragment "belongs" activity.

if want higher level sharing or network related data, suggest implement in bound service , have activities or fragments data directly service


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -