Create an android application to pass the data from one activity to another activity in…
- User
Create an android application to pass the data from one activity to another activity in the same application using intent.
- Assistant
Create an android application to pass the data from one activity to another activity in the same application using intent.
Creating an Android application that passes data from one activity to another using an Intent is a common task. Below, you can find a step-by-step guide to creating a simple app that demonstrates this feature.
This activity will allow the user to input data that will be sent to the second activity.
MainActivity.java (Java)
package com.example.intentexample;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextData;
private Button buttonSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextData = findViewById(R.id.editTextData);
buttonSend = findViewById(R.id.buttonSend);
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = editTextData.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("extra_data", data);
startActivity(intent);
}
});
}
}
MainActivity.kt (Kotlin)
package com.example.intentexample
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
class MainActivity : AppCompatActivity() {
private lateinit var editTextData: EditText
private lateinit var buttonSend: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
editTextData = findViewById(R.id.editTextData)
buttonSend = findViewById(R.id.buttonSend)
buttonSend.setOnClickListener {
val data = editTextData.text.toString()
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("extra_data", data)
startActivity(intent)
}
}
}
This activity will receive and display the data passed from the first activity.
SecondActivity.java (Java)
package com.example.intentexample;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
private TextView textViewReceivedData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textViewReceivedData = findViewById(R.id.textViewReceivedData);
String receivedData = getIntent().getStringExtra("extra_data");
textViewReceivedData.setText(receivedData);
}
}
SecondActivity.kt (Kotlin)
package com.example.intentexample
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class SecondActivity : AppCompatActivity() {
private lateinit var textViewReceivedData: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
textViewReceivedData = findViewById(R.id.textViewReceivedData)
val receivedData = intent.getStringExtra("extra_data")
textViewReceivedData.text = receivedData
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/editTextData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter some data" />
<Button
android:id="@+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Data" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/textViewReceivedData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
Make sure both activities are declared in the AndroidManifest.xml file:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
Now, run your application. You should be able to enter text in the first activity, click the "Send Data" button, and see the text displayed in the second activity.
That's it! You've successfully created an Android application that passes data between activities using an Intent.
Not yet included in a published release.