Passing objects in Android

In this tutorial, I will show you the method of Passing Objects in Android. Passing Objects in Android may be done for objects like Strings, User Input data, Web URLs etc.,

When we do Passing Objects in Android data from one Activity to another Activity values of Strings, User Input data or web URLs are passed from First Activity to Second Activity. Passing Objects in Android can be done by using Implicit Intent which is one of the Intent types.

An Implicit Intent is a type of Intent where we know the destination is. That is we know which activity we are going next.

The output of this method will be like below picture.

passing objects in Android

Procedure:

Note: Watch Below Video for better understanding of the code.

  • Create a new Android Studio Project
  • Add below line in dependencies of your App Gradle.

  • In activity_main.xml file copy below code by removing all existing code.

 

  • In above activity_main.xml file, I have given three input fields for Name, Email, and Phone number and a Button to Save the data. But no data is saved here. To button, we will add intent which will pass objects to another activity.
  • Now in MainActivity.java file paste below code in the onCreate() method.

  • Now Create a new class with name SecondActivity.java  and paste below code by removing everything except the first line

  • Next, add above Activity to you AndroidManifest file by pasting below line in <application> tag.

 

  • And create a new Layout file with name activity_second.xml and paste below code by replacing everything in it.

  • In above activity_second.xml  file, I added only two things one TextView to show values of objects passed from MainActivity.java and a Button to go back to previous activity.
  • Next, add below code to the onCreate() method of SecondActivity.java

  • Now run the code on Android device or emulator. Fill the form like below

  • Now press Save Button you will move to second activity and see output like below Image.

  • If you like this post share it. Because sharing is caring.