Fragment Communication in android

Embed Size (px)

Citation preview

  • 7/24/2019 Fragment Communication in android

    1/10

    Communication b/w Fragments

    Fragment is a peace of Activity in android.

    One Activity can have any no.of fragments. Each fragment have it's own

    functionality.

    All the fragmnets must be existed in one or more than one activity. Without

    activity we can't show the fragment in android application.

    Two frgments should never communicate directly.

    The control of android fragmentis handled by the activity which is havingthe fragment.

    The lifecycle of fragment is purely depends on activity lifecycle which is

    contains that fragment.

    !sing fragments we can utili"e the device screen for multiple functionalities

    shows at the same time.

    #n this Android Tutorialwe learn about how communicate two fragmentswithin the same activity. For Example i have one activity that activity display two

    differnet fragments. #n first fragment # will show the list of districts and in my

    second fragment #t will display the details of individual district. when tap on first

    fragment district list.

    $reate new Android Application pro%ect.

    activity_main.xml

    &inearayout

    xmlns(android)*http(++schemas.android.com+ap,+res+android*

    xmlns(tools)*http(++schemas.android.com+tools*

    android(layout-width)*match-parent*

    http://www.pcnmtutorialsc.com/http://www.pcnmtutorialsc.com/
  • 7/24/2019 Fragment Communication in android

    2/10

    android(layout-height)*match-parent*

    android(orientation)*hori"ontal*

    android(baselineAligned)*false*

    &fragment

    android(id)*/0id+listOfEmployees*

    android(name)*com.example.fragmentcommunication.fragments.istOfEmployeesF

    ragment*

    android(layout-width)*1dp*

    android(layout-height)*fill-parent*

    android(layout-weight)*2*+

    &fragment

    android(id)*/0id+employee3etais*

    android(name)*com.example.fragmentcommunication.fragments.Employee3etailsF

    ragment*

    android(layout-width)*1dp*

    android(layout-height)*fill-parent*

    android(layout-weight)*4*+

    &+inearayout

    The name attribute is indicates fragment class in your android application.

    andrid:laout_weightThis is the attribute to align the fragments in android

    activity. if you give layout-weight as 2 then it is first fragment it will show leftside

    fo your screen. #f you give layout-weight as 4 then it will show as second part in

    your activity. #t will show rightside of your screen.

    5ow crete a new layout for list of 3istricts. This is the fragment in your

    android activity. The layout name is fragment_list_fragment.xml

    &6xml version)*2.1* encoding)*utf78*6

  • 7/24/2019 Fragment Communication in android

    3/10

    &inearayout

    android(id)*/0id+listOfdists*

    xmlns(android)*http(++schemas.android.com+ap,+res+android*

    android(layout-width)*fill-parent*

    android(layout-height)*fill-parent*

    &ist9iew

    android(id)*/0id+list*

    android(layout-width)*match-parent*

    android(layout-height)*match-parent*

    &+ist9iew

    &+inearayout

    5ow crete another new layout for display individual district details. This is the

    fragment in your android activity. The layout name is

    fragment_details_fragment.xml

    &6xml version)*2.1* encoding)*utf78*6

    &inearayout

    xmlns(android)*http(++schemas.android.com+ap,+res+android*

    android(id)*/0id+details*

    android(layout-width)*match-parent*

    android(layout-height)*match-parent*

    android(orientation)*vertical*

    &Text9iew

    android(id)*/0id+details9iew*

    android(layout-width)*fill-parent*

    android(layout-height)*wrap-content*+

  • 7/24/2019 Fragment Communication in android

    4/10

    &+inearayout

    5ow create we need to create three two more classes for two fragment.

    The first fragment is ListFragment.java. This fragment displays the list of

    employees. And the second class is second fragment in our android application.

    That class name is DetailsFragment.java.

    $opy below code into your ListFragment.javaclass in your android

    application pro%ect.

    import %ava.util.Arrayist:

    import %ava.util.ist:

    import android.app.Activity:

    import android.os.;undle:

    import android.support.v

  • 7/24/2019 Fragment Communication in android

    5/10

    >

    private 9iew v:

    private static ist9iew lvOne:

    private ist&?tring listOf3istricts:

    private ArrayAdapter&?tring adapter:

    ist#tem?electedistener itemisterner:

    /Override

    public 9iew on$reate9iew@ayout#nflater inflater 9iew=roup container

    ;undle saved#nstance?tate

    >

    inflater )

    @ayout#[email protected][email protected]!T-#5FATEC-?EC9#$E:

    v ) [email protected] null false:

    addist#temsToist9iew@:

    initiali"eisteners@:

    return v:

    D

    /Override

    public void onAttach@Activity activity

    >

  • 7/24/2019 Fragment Communication in android

    6/10

    super.onAttach@activity:

    itemisterner ) @ist#tem?electedisteneractivity:

    D

    private void addist#temsToist9iew@

    >

    lvOne ) @ist9iewv.find9iew;y#[email protected]:

    listOf3istricts ) new Arrayist&?tring@:

    listOf3istricts.add@*?ri,a,ulam*:

    listOf3istricts.add@*9i%ayanagaram*:

    listOf3istricts.add@*9i"ag*:

    adapter ) new ArrayAdapter&?tring@getActivity@

    android.C.layout.simple-list-item-2 listOf3istricts:

    lvOne.setAdapter@adapter:

    D

    private void initiali"eisteners@

    >

    lvOne.setOn#tem$lic,istener@this:

    D

    public interface ist#tem?electedistener

    >

  • 7/24/2019 Fragment Communication in android

    7/10

    public void list#tem?electedistener@?tring name:

    D

    /Override

    public void on#tem$lic,@Adapter9iew&6 parent 9iew view int position long id

    >

    itemisterner.list#[email protected]@position:

    D

    D

    #n the above fragment we are use one Interfaceis

    calledistItem!electedistener. ;ecause We can not communicate directly two

    fragments in android if you want communicate two fragemnts. we need to create

    one interface in first Fragment and implement that interfac in Activity of that

    fragment. And then create one public setter method in second Fragement and

    assign the appropriate value to second fragment by using that public method.

    itemisterner " #istItem!electedistener$activity%This peace of line

    indicates the interface listerner register with the actiivty.

    itemisterner.listItem!electedistener#list&f'istricts.get#(osition$$%

    This peace of code indicates the value set as a perameter to method of interface.

    when implent the interface into activity. Then you must override this method in

    Activity register for this fragment.

    5ow create a new class with name 'etailsFragment.)ava#n this class it will

    display the related content. ;ased on selected items in the first fragment. The

    below code snippet is copy into your class.

    DetailsFragment.java

    pac,age com.example.fragmentcommunication:

  • 7/24/2019 Fragment Communication in android

    8/10

    import android.content.$ontext:

    import android.os.;undle:

    import android.support.v

    private static Text9iew tvOne:

    private 9iew v:

    $ontext context:

    private static final ?tring TA= ) *3etailsFragment*:

    public void get#tem9alue@?tring item5ame $ontext context

    >

    this.context ) context:

    tvOne.setText@*Welcome to ( *0item5ame:

    D

    /Override

    public 9iew on$reate9iew@ayout#nflater inflater 9iew=roup container

    ;undle saved#nstance?tate

    >

    inflater )

    @ayout#[email protected]?ystem?ervice@[email protected]!T-#5FATEC-?E

    C9#$E:

  • 7/24/2019 Fragment Communication in android

    9/10

    v ) [email protected] null false:

    tvOne ) @Text9iewv.find9iew;y#[email protected]:

    on$reate@saved#nstance?tate:

    return v:

    D

    D

    #n the above code snippet we are write one method (ublic void

    getItem*alue#!tring item+ame, Context context$. The usage of this method

    is to assign the selected value in the first fragment to second fragment using

    activity.

    5ow $oming to -ainActivity.)avawe will implement the interface. which is

    declared in first fragment and assign the value getting from first activity and send it

    to second fragment. ?o copy the below code snippet into-ainActivity.)ava

    pac,age com.example.fragmentcommunication:

    import

    com.example.fragmentcommunication.istFragment.ist#tem?electedistener:

    import android.os.;undle:

    import android.support.v

    public static final ?tring TA= ) *ainActiviyt*:

    /Override

    protected void on$reate@;undle saved#nstance?tate >

    super.on$reate@saved#nstance?tate:

    try

    >

  • 7/24/2019 Fragment Communication in android

    10/10

    [email protected]:

    D

    catch@Exception e

    >

    og.e@TA= e.to?tring@:

    D

    D

    /Override

    public void list#tem?electedistener@?tring name

    >

    3etailsFragment detailsFragment ) new 3etailsFragment@:

    detailsFragment.get#tem9alue@name this:

    D

    D