Communicate between activity in Android Studio  

Intents are the objects of android.content.Intent type. It can contain data via a Bundle. This data can be used by the receiving component.startActivity() method you can define that the intent should be used to start.

0 328 Technology | Android App
Tue Nov 13 , 2018

Step 1 : Create a new project name it IntentExample

Step 2 : Select Empty Activity

Step 3 : Inside main_activity.xml Add Button

Copy and paste code


<LinearLayout
<Button
  android:id="@+id/btnclick"
  android:text="Click Here"
  android:textcolor="#FF0000"
  android:layout_width="70dp"
  android:layout_height="70dp"
/>
</LinearLayout

Step 4 : Now go to MainActivity.java

Copy and paste code


Button btn; //make object of Button Class before oncreate method
btn=findViewById(R.id.btnclick); //find the btnclick id

//Add Button Click Event

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(MainActivity.this,NextActivity.class);

//Intent is used to redirect from one activity to another activity
//we have to make a object of Intent class and pass the current activity name and the redirect activity name

startActivity(intent);

//after that startactivity and pass the object of the Intent class
}
});

Step 5 : Now Add a new Activity and name it NextActivity

Build the project

Run


Subscribe & Get Regular Updates On Your E-mail