Monday, 4 February 2019

Intent



As we all know an application has more than one activity. Intent  is basically a type of message send by the current activity to android to start the another activity. Lets take an example to send message from one activity to another. Suppose the two activity names are  FirstActivity and SecondActivity.


Here are the steps

  1. Create a basic app with a single activity and layout.
  2. Add second activity  and layout.
  3. Get the first activity to call the second activity.
  4. Get the first activity  to pass data to the second activity.

For the FirstActivity.java file xml view will look like this.



When a user enter text in edittext and click send message button, two things will happen. First thing is it will move to SecondActivity and then it display the text in textview which a user enter in the edittext in FirstActivity.


Have a look on SecondActivity


Lets understand how exactly this happen. 


If you see the above code snippet, when the user click on sendmessage button, onSendMessage() method is called.

Inside the code, First activity create intent and tell android to start the Second Activity by calling startActivity(intent) and passing intent to it.

Adding to this point activity added  message also for the second activity by calling "intent.putExtra()".


Look the below diagram for better understanding



Now Let see how the SecondActivity response for the incoming message.