How to pass an object from one Activity to another in Android?

AndroidApps/ApplicationsMobile Development

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 +"\n" + sports.Proffession + "\n" + sports.Position+ "\n" + 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.

raja
Published on 02-Aug-2019 10:32:51
Advertisements