Android pass object to activity

There are few ways to pass or send an object to an activity. In this Android tip, I am going to show you how to achieve this goal using Serialization method.
In the Serialization method, the class that is used to create the object must implement the Serializable interface. It does not require you to override any method. The object will be put in a Bundle object using putSerializable() method. Before you call the startActivity() method to send the object to the target activity, you need to put the Bundle object in an Intent object using putExtra() method.
In the target activity, you can receive the Bundle object using the getBundleExtra() method. From the received bundle, you will use the getSerializable() method to get the object.




Now to have an example application on passing an object to an activity, you create a new Android project. Then you create another activity called SecondActivity. In the layout file of the SecondActivity, you add a TextView. It is used to display data of the object sent from the MainActivity.

<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"
   tools:context="com.example.myapp.SecondActivity"
   android:orientation="vertical"
   >

   <TextView
     android:id="@+id/txtview"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:singleLine="false"
     android:text="@string/hello_world" />

</LinearLayout>


In the src folder, you create a class called Course that implements the Serializable interface. An object of the class will be sent from the MainActivity activity to the SecondActivity activity. Here is the code written in the onCreate() method of the MainActivity class to send the object to the SecondActivity.

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent=getIntent();
Bundle b=intent.getBundleExtra(MainActivity.bKey);
Course c=(Course)b.getSerializable(MainActivity.sKey);
if(c!=null){
String str="Code: "+c.getCode()+"\r\n"+"Number of Credits: "+c.getCredit()+"\r\n"+"Start date:"+c.getSDate()+"\r\n"+"Endate: "+c.getEDate();
TextView tv=(TextView)findViewById(R.id.txtview);
tv.setText(str);
}
}

The code in the onCreate() method of the SecondActivity to receive the object is shown below.


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent=getIntent();
Bundle b=intent.getBundleExtra(MainActivity.bKey);
Course c=(Course)b.getSerializable(MainActivity.sKey);
if(c!=null){
String str="Code: "+c.getCode()+"\r\n"+"Number of Credits: "+c.getCredit()+"\r\n"+"Start date:"+c.getSDate()+"\r\n"+"Endate: "+c.getEDate();
TextView tv=(TextView)findViewById(R.id.txtview);
tv.setText(str);
}
}

Android-pass-object-to-activity

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