In an Android application, how do you start a new activity (GUI) when a button in another activity is clicked, and how do you pass data between these two activities?
You can try this code:
Intent myIntent = new Intent();
FirstActivity.this.SecondActivity(myIntent);
The way to start new activities is to broadcast an intent, and there is a specific kind of intent that you can use to pass data from one activity to another. My recommendation is that you check out the Android developer docs related to intents; it's a wealth of info on the subject, and has examples too.
Implement the View.OnClickListener interface and override the onClick method.
ImageView btnSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search1);
ImageView btnSearch = (ImageView) findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearch: {
Intent intent = new Intent(Search.this,SearchFeedActivity.class);
startActivity(intent);
break;
}
Start another activity from this activity and u can pass parameters via Bundle Object also.
Intent intent = new Intent(getBaseContext(), YourActivity.class);
intent.putExtra("USER_NAME", "xyz@gmail.com");
startActivity(intent);
Retrive data in another activity (YourActivity)
String s = getIntent().getStringExtra("USER_NAME");
When user clicks on the button, directly inside the XML like that:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextButton"
android:onClick="buttonClickFunction"/>
Using the attribute android:onClick
we declare the method name that has to be present on the parent activity. So I have to create this method inside our activity like that:
public void buttonClickFunction(View v)
{
Intent intent = new Intent(getApplicationContext(), Your_Next_Activity.class);
startActivity(intent);
}
// In Kotlin , you can do as /* In First Activity, let in activity layout there is button which has id as button. Suppose I have to pass data as String type from one activity to another */
val btn = findViewById<Button>(R.id.button)
btn.setOnClickListener {
val intent = Intent(baseContext, SecondActivity::class.java).apply {
putExtra("KEY", data)
}
startActivity(intent)
}
// In Second Activity, you can get data from another activity as
val name = intent.getStringExtra("KEY")
/* Suppose you have to pass a Custom Object then it should be Parcelable. let there is class Collage type which I have to pass from one activity to another */
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
@Parcelize
class Collage(val name: String, val mobile: String, val email: String) : Parcelable
/* Activity First , let here data is Collage type. which I have to pass to another activity. */
val btn = findViewById<Button>(R.id.button)
btn.setOnClickListener {
val intent = Intent(baseContext, SecondActivity::class.java).apply {
putExtra("KEY", data)
}
startActivity(intent)
}
// then from second Activity we will get as
val item = intent.extras?.getParcelable<Collage>("KEY")