Pages

Monday, April 25, 2016

Android Fragment Example: Inter-Fragment Communication

MainActivity.java

package kanta.interfragmentsave; 
 
import android.app.Activity; 
import android.app.FragmentManager; 
import android.os.Bundle; 
 

public class Main extends Activity implements communicator 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    } 
 
 
    @Override 
    public void respond(int data) { 
        FragmentManager manager=getFragmentManager(); 
        FragB fragB= (FragB) manager.findFragmentById(R.id.fragment2); 
 
        fragB.changedata(""+data+""); 
    } 
} 
FragmentA.java


package kanta.interfragmentsave; 
 
import android.app.Fragment; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
 

public class FragA extends Fragment 
{ 
    Button b; 
    communicator comm; 
    int count=0; 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
 
        if(savedInstanceState==null) 
        { 
             count=0; 
        } 
        else 
        { 
           count=savedInstanceState.getInt("count",0); 
        } 
    } 
 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
        return inflater.inflate(R.layout.frag_a,container,false); 
    } 
 
    @Override 
    public void onSaveInstanceState(Bundle outState) { 
        super.onSaveInstanceState(outState); 
        outState.putInt("count",count); 
    } 
 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
        super.onActivityCreated(savedInstanceState); 
        comm= (communicator) getActivity(); 
        b=(Button)getActivity().findViewById(R.id.button); 
        b.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View v) 
            { 
                count++; 
                comm.respond(count); 
            } 
        }); 
    } 
} 

FragmentB.java
package kanta.interfragmentsave; 
 
import android.app.Fragment; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
 
/** 
 * Created by kanta on 25-Apr-16. 
 */ 
public class FragB extends Fragment 
{ 
    TextView textView; 
    String data; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
        return inflater.inflate(R.layout.frag_b,container,false); 
    } 
 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
        super.onActivityCreated(savedInstanceState); 
        textView=(TextView)getActivity().findViewById(R.id.textView); 
 
    } 
 
 
 
    public void changedata(String data) 
    { 
        //this.data=data; 
        textView.setText(data); 
    } 
} 

communicator.java
package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
   }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/fragment"
android:name="kanta.interfragmentsave.FragA"
android:layout_width="match_parent"
android:layout_height="100dp" />
<fragment
android:id="@+id/fragment2"
android:name="kanta.interfragmentsave.FragB"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp" />
</LinearLayout>
fragment_a.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_dark" android:gravity="center" android:orientation="vertical">
<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Change" /></LinearLayout>

fragment_b.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
</LinearLayout>

No comments:

Post a Comment