How to communicate between activities in Android?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our W3Make Forum to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
1.Using Intents:
Start an activity by creating an Intent and passing data through Intent extras (using putExtra() methods). The launched activity can retrieve the data using getIntent() and get the extras.
You can also use startActivityForResult() to start an activity and receive a result back from it.
2.Using Bundles:
Bundles are used to pass data between activities using the putExtras() method of an Intent. The receiving activity can retrieve the data using getIntent().getExtras().
3.Using Parcelable or Serializable:
Implement the Parcelable or Serializable interfaces in your custom class to make it serializable. Then you can put instances of the class directly in the Intent extras or Bundle to pass them between activities.
4.Using a Shared ViewModel:
Create a ViewModel instance and share it between multiple activities. The activities can observe changes in the ViewModel to communicate and share data.
5.Using a Singleton or Application class:
Create a Singleton class or extend the Application class to store and share data globally across activities.
6.Using Local Broadcasts:
Send broadcasts using the LocalBroadcastManager class and register BroadcastReceivers in activities to listen for and receive those broadcast
Using intents
if you want to interact with different activities then you can you Intent method for the
this method has parameter as startActivity to desitination activity name
i.e e.g – you want to move from activity 1 to activity 2
you can use code as
val intent = Intent(this,activity 2 :: class java )
start(intent)
here start method is called to start the intent activity
By using intent: