Android send intent from fragment to activity

In the previous post, you learnt how to create fragments and replace one fragment by another fragment.

In this Android tip, I will show you how to send an intent from a fragment to an activity. Sending an intent from a fragment to an activity is almost the same as sending an intent from an activity to another activity except you need to pass a reference of the fragmen's hosting activity to the constructor of the Intent class. You can make the reference to the hosting activity of the fragment by calling the getActivity() method.




Now to have a workable example application, create a new Android project. Then modify the activity_main.xml to add a fragment as shown below.

activity_main.xml
<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" >

   <fragment
     android:id="@+id/MyFragment"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     class="com.example. androidproject.MyFragment" >
      
    </fragment>
  
  

</RelativeLayout>


Then create MyFragment class that extendes the Fragment class. The layout file for this fragment is my_fragment.xml. There are one EditText and on Button in the layout file. In the onStart() method of the MyFragment class, you need code to send data and open the second activity.

MyFragment.java

package com.example.androidproject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class MyFragment 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.my_fragment, container, false);
}

public void onStart(){
super.onStart();
Button bt=(Button)context.findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener(){
public void onClick(View view){
EditText et= (EditText)context.findViewById(R.id.txt_edit);
//create an Intent object
Intent intent=new Intent(context, SecondActivity.class);
//add data to the Intent object
intent.putExtra("text", et.getText().toString());
//start the second activity
startActivity(intent);
}

});
}

}


my_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" >
  
  
   <EditText
     android:id="@+id/txt_edit"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:inputType="text"
     android:hint="Type your text here"
    
     />
   <Button
     android:id="@+id/bt"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Send it to the second activity"
       />

</LinearLayout>


You will create another activity called SecondActivity to receive the intent sent from the fragment.

SecondActivity.java

package com.example.androidproject;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class SecondActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}

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

public void onStart(){
super.onStart();
TextView tv=(TextView)findViewById(R.id.txt_view);
//show text in the Intent object in the TextView
tv.setText("Received text:"+getIntent().getStringExtra("text"));
}

}


Here is the content of activity_second.xml file that is the layout file of the SecondActivity.

<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=".SecondActivity" >

   <TextView
     android:id="@+id/txt_view"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/hello_world" />

</RelativeLayout>


Before running the example application, you need to make change to the MainActivity by extending the FragmentActivity class instead of extending the Activity class.

Android-send-intent-from-fragment-to-activity

Posted by: Dara | post date: 07-22-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.