Pages

Thursday 6 March 2014

Android: Passing Messages between Activities

Intents are the messages which are passed between Activities, Broadcast Receivers, Services and Content Providers. So intent can be used to start a new Activity and to pass a message between activities. In our application first activity will pass some message to second activity, which will be simply retrieved and displayed by the second activity.

Output will be as:





Xml files: res---layout
activity_main.xml
 <LinearLayout 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:orientation="vertical"  
   android:padding="10dp"  
    >  
   <EditText   
     android:id="@+id/etmessage"  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:hint="Enter Message"  
     />  
   <Button   
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Send"  
     />  
   </LinearLayout>  

message.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"   
   android:padding="10dp"  
   >  
   <TextView   
     android:id="@+id/tvMessage"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:textSize="24dp"  
     android:textColor="#FF0000"  
     android:layout_gravity="center"  
     />  
 </LinearLayout>  

Java files:
MainActivity.java
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.content.Intent;  
 import android.view.Menu;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.Button;  
 import android.widget.EditText;  
 public class MainActivity extends Activity implements OnClickListener {  
      EditText et;  
      Button b1;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main); //referencing UI  
           et=(EditText) findViewById(R.id.etmessage);//referencing EditText  
           b1=(Button) findViewById(R.id.button1);//referencing Button  
           b1.setOnClickListener(this); //adding click event  
      }  
      @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;  
      }  
      @Override  
      public void onClick(View v) {  
           // TODO Auto-generated method stub  
           switch(v.getId()){  
           case R.id.button1:  
                String message=et.getText().toString();  
                Intent i=new Intent(this,Message_class.class);//Intent used to start a new Activity  
                Bundle extras=new Bundle();  
                extras.putString("key", message);//binding data to bundle  
                i.putExtras(extras);//Adding extras(bundle) to intent  
                startActivity(i);//starting new Activity  
                finish();//finishing current activity  
                break;  
           }  
      }  
 }  
Here we have simply used Bundle that binds our data with Intent.
After adding bundle to an intent, startActivity() method is used to start a new Activity.

Message_class.java
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.widget.TextView;  
 public class Message_class extends Activity {  
   TextView tv;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           // TODO Auto-generated method stub  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.message);  
           tv=(TextView) findViewById(R.id.tvMessage);  
           Bundle extras=this.getIntent().getExtras();//retrieving bundle from intnet  
           if(extras!=null){  
                String msg=extras.getString("key");//retrieving message from bundle  
                tv.setText(msg);  
           }  
      }  
 }  

Manifest file:
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.example.message_example"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="17" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.example.message_example.MainActivity"  
       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=".Message_class"  
       android:label="New Activity"  
       ></activity>  
   </application>  
 </manifest>  
Here we have added our new activity i.e Message_class to our manifest file.
Feel free to ask me your doubts in the comments below.

No comments:

Post a Comment