- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to pass an object from one Activity to another in Android?
This example demonstrates how do I pass an object from one Activity to another in android.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click here to pass object to Second Activity!" android:id="@+id/button" android:layout_centerInParent="true" /> </RelativeLayout>
Step 3 − Create a java class and add the following code in Character.java
import java.io.Serializable; public class Character implements Serializable { String name, Proffession, Position; String[] abilities; public Character(String name, String proffession, String position, String[] abilities) { this.name = name; Proffession = proffession; Position = position; this.abilities = abilities; } }
Step 4 − Add the following code to MainActivity.java
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), SecondActivity.class); Character sports = new Character("CR7", "Football", "Left Winger", new String[]{"Best Freeckicker in the World"}); intent.putExtra("Character", sports); startActivity(intent); } }); } }
Step 5 − Create an emty activity, name it as SecondActivity and add the following code in SecondActivity.java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import java.util.Arrays; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Character sports = (Character)getIntent().getSerializableExtra("Character"); TextView textView = findViewById(R.id.textView); textView.setText(sports.name +"
" + sports.Proffession + "
" + sports.Position+ "
" + Arrays.toString(sports.abilities)); } }
Step 6 − Add the following code in activity_second.xml
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".SecondActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" android:textSize="20sp" android:layout_centerInParent="true"/> </RelativeLayout>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −
Click here to download the project code.
- Related Articles
- How to pass an image from one activity another activity in Android?
- How do I pass an object from one activity to another on Android using Kotlin?
- How to pass values from one activity to another in Android?
- How to pass multiple data from one activity to another in Android?
- How to pass a String from one Activity to another in Android?
- How to pass an image from one activity to another activity in Kotlin?
- How to pass a String from one Activity to another Activity in Android using Kotlin?
- How to pass data from one activity to another in Android using shared preferences?
- How to send an object one Android activity to another using Intents?
- How to pass an arrayList to another activity using intents in Android?
- How do I send an object from one Android Activity to another using Intents in Kotlin?
- How to pass an arrayList to another activity using intents in Android Kotlin?
- How to pass data from one fragment to another fragment in android?
- How to send data from one activity to another in Android using intent?
- How to send data from one activity to another in Android using bundle?
