We are back, This time with more on android. In this article i will share my experience of using intents and try to simplify things.
Whenever one calls a function, he/she can pass arguments to the function. Similarly when one calls another activity from an activity, how are the arguments passed to the called application ? Or even if two activities want to talk ( exchange messages ), how do they achieve it ?. How does an activity start another activity ? The answer to these questions is intents !
Intent is basically a carrier for your message. You can send messages to the other activity via intents. The way i most prefer is to put it into extras and then get the bundle
eg.
To get the sent information on other side, one can follow the following way.
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("Name");
int age = bundle.getInt("Age");
and so on....
So, We now know how to pass messages. One thing here, to ponder over, is that an activity can call an activity/service as a sequel or may be it is expecting a reply from the called activity. A rough example of a sequel calling can be an ImageDisplayActivity or a SlideShowActivity. Example of calling an activity, when expecting a reply, is calling SelectImageActivity which displays images of a given folder and then replies the imageFile which was selected by the user. Another can be of calling MakeTransactionActivity which tells if the transaction was successfully completed or not, etc. The above two methods of initiating another activity can be done by
Also one can broadcast some info or object using intents, to other Broadcast Receivers, by the means of sendBroadcast() and other similar methods. The broadcast listeners have to override their onReceive() method.
For more information on broadcasting intents refer here
For initiating other standard activites, just call startActivity with the activity Action. eg the most common is to initiate a phone call. This is a way how it can be done.
Intent intent = new Intent ( Intent.ACTION_CALL ) ;
intent.setData( Uri.parse("tel:999666333") ) ;
startActivity( intent ) ;
Hope this helps :)
Whenever one calls a function, he/she can pass arguments to the function. Similarly when one calls another activity from an activity, how are the arguments passed to the called application ? Or even if two activities want to talk ( exchange messages ), how do they achieve it ?. How does an activity start another activity ? The answer to these questions is intents !
Intent is basically a carrier for your message. You can send messages to the other activity via intents. The way i most prefer is to put it into extras and then get the bundle
eg.
Intent intent = new Intent() ;
intent.putExtra("Name", "Saket" ) ; //These are key-value pairs with string key.
intent.putExtra("Age", 20 ) ; //Value can of any type
To get the sent information on other side, one can follow the following way.
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("Name");
int age = bundle.getInt("Age");
and so on....
So, We now know how to pass messages. One thing here, to ponder over, is that an activity can call an activity/service as a sequel or may be it is expecting a reply from the called activity. A rough example of a sequel calling can be an ImageDisplayActivity or a SlideShowActivity. Example of calling an activity, when expecting a reply, is calling SelectImageActivity which displays images of a given folder and then replies the imageFile which was selected by the user. Another can be of calling MakeTransactionActivity which tells if the transaction was successfully completed or not, etc. The above two methods of initiating another activity can be done by
- startActivity()
- startActivityForResult().
Also one can broadcast some info or object using intents, to other Broadcast Receivers, by the means of sendBroadcast() and other similar methods. The broadcast listeners have to override their onReceive() method.
For more information on broadcasting intents refer here
For initiating other standard activites, just call startActivity with the activity Action. eg the most common is to initiate a phone call. This is a way how it can be done.
Intent intent = new Intent ( Intent.ACTION_CALL ) ;
intent.setData( Uri.parse("tel:999666333") ) ;
startActivity( intent ) ;
Hope this helps :)