android - Fragment to Activity to Fragment Communication -


i have 2 frame layouts(containers) in main.xml contains 2 fragments. want pass video url fragmentone activity , activity fragmenttwo . unable url in second fragment. below code,please me correct solution.

activity_main.xml

<relativelayout     android:layout_width="match_parent"     android:layout_height="match_parent">      <framelayout         android:layout_width="match_parent"         android:id="@+id/top"         android:layout_height="350dp">      </framelayout>      <framelayout         android:layout_width="340dp"         android:layout_margintop="20dp"         android:id="@+id/frgten"         android:layout_below="@+id/top"         android:layout_marginleft="20dp"         android:layout_marginright="32dp"         android:layout_height="130dp"/>   </relativelayout> 

fragment_one.xml

<relativelayout     android:layout_width="match_parent"     android:layout_height="match_parent">       <relativelayout         android:layout_width="match_parent"         android:layout_margintop="15dp"         android:id="@+id/rl1"         android:layout_height="wrap_content">          <textview             android:layout_width="100dp"             android:id="@+id/frg1"             android:background="#c1a499"             android:text="play"             android:textalignment="center"             android:layout_marginleft="25dp"             android:layout_height="100dp"             android:autolink="web">          </textview>          <textview             android:layout_width="100dp"             android:id="@+id/frg2"             android:layout_marginleft="10dp"             android:background="#c1a499"             android:text="play"             android:textalignment="center"             android:layout_torightof="@+id/frg1"             android:layout_height="100dp"             android:autolink="web">          </textview>          <textview             android:layout_width="100dp"             android:id="@+id/frg3"             android:background="#c1a499"             android:text="play"             android:textalignment="center"             android:layout_marginleft="10dp"             android:layout_torightof="@+id/frg2"             android:layout_height="100dp"             android:autolink="web">          </textview>      </relativelayout>      <relativelayout         android:layout_width="match_parent"         android:id="@+id/rl2"         android:layout_margintop="15dp"         android:layout_below="@+id/rl1"         android:layout_height="wrap_content">          <textview             android:layout_width="100dp"             android:id="@+id/frg4"             android:background="#c1a499"             android:text="play"             android:textalignment="center"             android:layout_marginleft="25dp"             android:layout_height="100dp"             android:autolink="web">          </textview>          <textview             android:layout_width="100dp"             android:id="@+id/frg5"             android:background="#c1a499"             android:text="play"             android:textalignment="center"             android:layout_marginleft="10dp"             android:layout_torightof="@+id/frg4"             android:layout_height="100dp"             android:autolink="web">          </textview>          <textview             android:layout_width="100dp"             android:id="@+id/frg6"             android:background="#c1a499"             android:text="play"             android:textalignment="center"             android:layout_marginleft="10dp"             android:layout_torightof="@+id/frg5"             android:layout_height="100dp"             android:autolink="web">          </textview>      </relativelayout>      <relativelayout         android:layout_width="match_parent"         android:id="@+id/rl3"         android:layout_below="@+id/rl2"         android:layout_margintop="15dp"         android:layout_height="wrap_content">          <textview             android:layout_width="100dp"             android:id="@+id/frg7"             android:background="#c1a499"             android:text="play"             android:textalignment="center"             android:layout_marginleft="25dp"             android:layout_height="100dp"             android:autolink="web">          </textview>          <textview             android:layout_width="100dp"             android:id="@+id/frg8"             android:background="#c1a499"             android:text="play"             android:textalignment="center"             android:layout_marginleft="10dp"             android:layout_torightof="@+id/frg7"             android:layout_height="100dp"             android:autolink="web">          </textview>          <textview             android:layout_width="100dp"             android:id="@+id/frg9"             android:background="#c1a499"             android:text="play"             android:textalignment="center"             android:layout_marginleft="10dp"             android:layout_torightof="@+id/frg8"             android:layout_height="100dp"             android:autolink="web">          </textview>      </relativelayout>  </relativelayout> 

fragment_two.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="match_parent"     android:layout_height="match_parent">      <videoview         android:layout_width="match_parent"         android:id="@+id/video"         android:layout_height="match_parent" />  </linearlayout> 

mainactivity

import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.support.v7.app.appcompatactivity; import android.os.bundle;  public class mainactivity extends appcompatactivity implements fragmentone.communicationchannel {      private fragmentone sendfragment=new fragmentone();     private fragmenttwo getfragment=new fragmenttwo();      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          fragmentmanager manager = getsupportfragmentmanager();         fragmenttransaction transaction = manager.begintransaction();         transaction.add(r.id.top,sendfragment);         transaction.add(r.id.frgten,getfragment);         transaction.commit();     }      @override     public void setcommunication(string url) {         if(null != getfragment  && getfragment.isinlayout())         {             getfragment.storerecievedurl(url);         }      } } 

fragmentone

import android.app.activity; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview;   /**  * simple {@link fragment} subclass.  */ public class fragmentone extends fragment {      private communicationchannel mcommchlistner=null;       @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         final view view = inflater.inflate(r.layout.fragment_one, container, false);          final textview t1 = (textview) view.findviewbyid(r.id.frg1);          t1.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 string url="http://www.androidbegin.com/tutorial/androidcommercial.3gp";                 sendurl(url);             }         });         return view;     }      interface communicationchannel     {         public void setcommunication(string url);     }     @override     public void onattach(activity activity)     {         super.onattach(activity);         if(activity instanceof communicationchannel)         {             mcommchlistner = (communicationchannel)activity;         }         else         {             throw new classcastexception();         }      }      public void sendurl(string url){           mcommchlistner.setcommunication(url);     }  } 

fragmenttwo

import android.os.bundle; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup;   /**  * simple {@link fragment} subclass.  */ public class fragmenttwo extends fragment {       public fragmenttwo() {         // required empty public constructor     }       @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         final view view= inflater.inflate(r.layout.fragment_two, container, false);           return view;     }      void storerecievedurl(string url){         string videourl = url;      }  } 

i didnt try running code think prob setcommunication method in activity.

i dont think how should instance of fragment. if see in google example in documentation here , youl see need instance of fragment this

@override     public void setcommunication(string url) {         fragmenttwo  getfragmentinstance = (fragmenttwo )                 getsupportfragmentmanager().findfragmentbyid(r.id.frgten);         if(getfragmentinstance!=null)         {             getfragmentinstance.storerecievedurl(url);         }      } 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -