Welcome to iBase Technology! In this video, I'm going to show you how to transfer data from one activity to another in an Android app. This is a fundamental concept that you'll use in almost every Android project. Let's get started!
Step-by-Step Guide:
Create the Activities:
Make sure you have two activities in your project. For this example, let's name them MainActivity and SecondActivity.
Define the Data to Transfer:
Decide what data you need to transfer. It could be a string, an integer, or even an object.
Passing Data Using Intent:
In MainActivity, create an Intent to start SecondActivity and add the data you want to transfer.
// MainActivity.java
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key", "Hello, Second Activity!");
startActivity(intent);
Retrieve Data in Second Activity:
In SecondActivity, retrieve the data from the intent.
// SecondActivity.java
Intent intent = getIntent();
String data = intent.getStringExtra("key");
// Now you can use the data variable in your SecondActivity
Handling Different Data Types:
You can pass different types of data using the appropriate putExtra methods.
intent.putExtra("intKey", 123);
intent.putExtra("booleanKey", true);
And retrieve them in SecondActivity:
int intValue = intent.getIntExtra("intKey", 0);
boolean boolValue = intent.getBooleanExtra("booleanKey", false);
Passing Complex Data:
For objects, you need to make the object class implement Serializable or Parcelable.
// Example object
public class MyObject implements Serializable {
private String name;
private int age;
// getters and setters
}
Pass the object:
MyObject obj = new MyObject("John", 25);
intent.putExtra("myObject", obj);
Retrieve the object:
MyObject myObject = (MyObject) getIntent().getSerializableExtra("myObject");
🌟 Stay connected with iBase across all platforms!
📸 Instagram: / ibase_techn. .
💻 Facebook: / ibasetechnol. .
📹 YouTube: / @ibasetechnol. .
🖥️Telegram: https://t.me/+Nc9Z2tVl-gRlMGU1
📚Linkedin: / ibas. .
🔖iBase App: https://play.google.com/store/apps/de...
🎉 Follow me for exclusive content and updates!