Home / Android / Intent case 4 in android

Intent case 4 in android

Intent in android development means to pass messages between different components so that different activities can communicate between each other. There are 4 cases in intent in android and this ones to describe intent case 4 in android.

Intent i=new Intent(getApplicationContext(),B.class);
i.putExtra("Req",e1.getText().toString());
startActivityForResult(i, 2);

Here are two classes that communicate to each other using intent:

  1. B.Java
  2. Intentfourthcaseactivity.Java

Download Fourth case of intent in android – full .zip package for eclipse.

Click Here- Intentfourthcase.zip

 

IntentFourthCaseActivity.Java

package gk.l;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class IntentfourthcaseActivity extends Activity {
    /** Called when the activity is first created. */
    EditText e1;
    Button b1;
    TextView t1;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    e1=(EditText)findViewById(R.id.editText1);
    b1=(Button)findViewById(R.id.button1);
    t1=(TextView)findViewById(R.id.textView1);
    b1.setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(getApplicationContext(),B.class);
			i.putExtra("Req",e1.getText().toString());
			startActivityForResult(i, 2);
		}
	});
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		String strrr=data.getStringExtra("respo");
		t1.setText(strrr);
	}

}

B.Java

package gk.l;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class B extends Activity {
    /** Called when the activity is first created. */
    EditText e1;
    Button b1;
    TextView t1;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other);
    e1=(EditText)findViewById(R.id.editText1);
    b1=(Button)findViewById(R.id.button1);
    t1=(TextView)findViewById(R.id.textView1);
    Intent i=getIntent();
    String strr=i.getStringExtra("Req");
    t1.setText(strr);
    b1.setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent();
			i.putExtra("respo", e1.getText().toString());
			setResult(3, i);
			finish();
		}
	});
	}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send request" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

 OUTPUT:

 

intent case 4 in android
Case Four for Intent in android

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.