Android passing variables from activity to fragment

When you use Fragment to create dynamic and multi-pane UI applications in Android, you might want to pass variables from an activity to a fragment so that they can be used in that fragment. In this Android tip, I am going to show you how to accomplish this task.

Now to have a workable application to demonstrate the task, create a new Android project. Then modify the activity_main.xml file to add FrameLayout as shown below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

  <FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"

/>
  
  

</RelativeLayout>


You will create TestFragment class that extends the Fragment class. The layout file for this fragment is test_fragment.xml. There are one TextView in the layout file. The TextView displays the message sent from the MainActivity activity. In the TestFragment class, I create a static method called newInstance that can be called to create a new instance of TestFragment and supply its construction argument. The data stored in the Bundle object that passed to the TestFragment instance by the setArguments remains available after the fragment is re-created. The setArguments method can only be called before the Fragment is attached to the Activity.  

TestFragment.java

 package com.example.androidproject;

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;

public class TestFragment extends Fragment {
Activity context;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context=getActivity();
//Inflate the layout for this fragment
return inflater.inflate(R.layout.test_fragment, container, false);
}

public static FirstFragment newInstance(String message){
FirstFragment ff=new FirstFragment();
//Supply the construction argument for this fragment
Bundle b=new Bundle();
b.putString("mess", message);
ff.setArguments(b);
return(ff);
}

public void onStart(){
super.onStart();
TextView tv=(TextView)context.findViewById(R.id.txt_view);
//read argument data and show it in the TextVview
tv.setText(getArguments().getString("mess"));
}

}

test_fragment.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" >
  
  
   <TextView
     android:id="@+id/txt_view"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:padding="10dp"
     />
  
</LinearLayout>






In the ActivityMain class, you need code to display the TestFragment fragment and pass a variable to it as shown below.

 package com.example.androidproject;

import java.util.Calendar;

import android.os.Bundle;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
public class MainActivity extends FragmentActivity{

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showFragment("This message is sent from MainActivity.");


}

public void showFragment(String mess){

//create FragmentTransaction instance
FragmentTransaction transact=getSupportFragmentManager().beginTransaction();
//create an instance of the TestFragment
   FirstFragment ff=FirstFragment.newInstance(mess);
//add fragment to transaction
transact.add(R.id.fragment_container,ff, "ff");
//commit the transaction
transact.commit();
  

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);

return true;
}
}

Android-passing-variables-from-activity-to-fragment

Posted by: Dara | post date: 07-24-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.