Sending and receiving data to and from Intents

Here I created a simple application for my assignment which sends data(Name) from one intent to another intent and the sencond intent sends feedback data (Grade) to first intent. Android developer newbies will find this tutorial quite helpful.

One thing that noticed my mind is that often we get that ‘Android app was closed unexpectedly….’ error. This is usually if your app returns some error like null pointer exceptions or something else. I was stuck at a point which worth sharing. The activities in the category should look something like this:

</pre>
<application android:icon="@drawable/icon" android:label="@string/app_name">
 <activity android:name=".GradeInquiryActivity"
 android:label="@string/app_name">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 <activity android:name=".Activity2"
 android:label="this is act 2">
 <intent-filter>
 <action android:name="edu.utep.cs.cs4390.grade.ACTIVITY2" />
 <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>
 </activity>

</application>
<pre>

Mark the difference between LAUNCHER and DEFAULT. The second activity has category name as DEFAULT and the main activity has it as LAUNCHER.

My main activity class named GradeInquiryActivity.java looks like this.


package edu.utep.cs.cs4390.grade;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class GradeInquiryActivity extends Activity {

 public int request_Code=1;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 Button btn= (Button) findViewById(R.id.btn_OK);

 btn.setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View v) {

 Intent data = new Intent();
 // TODO Auto-generated method stub
 EditText txEditText=(EditText) findViewById(R.id.txt_username);
 data.setData(Uri.parse(txEditText.getText().toString()));
 //setResult(RESULT_OK,data);

 Intent i= new Intent("edu.utep.cs.cs4390.grade.ACTIVITY2");
 Bundle extras= new Bundle();
 extras.putString("Name", data.getData().toString());
 i.putExtras(extras);
 startActivityForResult(i, 1);
 //Toast.makeText(getBaseContext(), data.getData().toString(), Toast.LENGTH_SHORT).show();

 }
 });
 }

 public void onActivityResult(int requestCode,int resultCode,Intent data){
 if(requestCode == request_Code){
 if(resultCode == RESULT_OK){
 Toast.makeText(this, data.getData().toString(), 100).show();
 }
 }
 }

}

The second activity class name Activity2.java look like this


package edu.utep.cs.cs4390.grade;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Activity2 extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity2);

String name = "NO name received";
 Bundle extras = getIntent().getExtras();
 if (extras != null) {
 name = extras.getString("Name");
 }
 Toast.makeText(getBaseContext(), name, Toast.LENGTH_SHORT).show();

TextView txtVw = (TextView) findViewById(R.id.txVw);
 txtVw.setText((CharSequence) name);

// sent data to previous activity
 Button btnA = (Button) findViewById(R.id.btn_A);
 Button btnB = (Button) findViewById(R.id.btn_B);

btnA.setOnClickListener(new View.OnClickListener() {

@Override
 public void onClick(View v) {

Intent data = new Intent();
 data.setData(Uri.parse("A"));
 setResult(RESULT_OK, data);
 finish();
 // Toast.makeText(getBaseContext(), data.getData().toString(),
 // Toast.LENGTH_SHORT).show();

}
 });

 btnB.setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 Intent data = new Intent();
 data.setData(Uri.parse("B"));
 setResult(RESULT_OK, data);
 finish();

 }
 });
 }
}

Here is some of the screenshots of the project:

Screenshots of the GradeInquiry Project
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s