INTENTS Babylon University , IT College , SW Dep. , Android Assist. Lecturer : Wadhah R. Baiee (2014) Ref: Wei-Meng Lee, “BEGINNING ANDROID™ 4 APPLICATION DEVELOPMENT “, Ch2 , John Wiley & Sons , 2012 Resolving Intent Filter Collision In the previous section, you learned that the <intentfilter> element defines how your activity can be invoked by another activity. What happens if another activity (in either the same or a separate application) has the same filter name? For example, suppose your application has another activity named Activity3, with the following entry in the AndroidManifest.xml file: Resolving Intent Filter Collision Resolving Intent Filter Collision If you call the startActivity() method with the following intent, then the Android OS will display a selection of activities : If you check the “Use by default for this action” item and then select an activity, then the next time the intent “net.learn2develop.SecondActivity” is called again, it will launch the previous activity that you have selected. Resolving Intent Filter Collision Returning Results from an Intent The startActivity() method invokes another activity but does not return a result to the current activity. If you need to pass data back from an activity, you should instead use the startActivityForResult() method. Returning Results from an Intent Returning Results from an Intent Add the following statements in bold to the secondactivity.xml file Returning Results from an Intent Add the following statements in bold to the SecondActivity.java Returning Results from an Intent Add the following statements in bold to the UsingIntentActivity.java Returning Results from an Intent In addition to passing in an Intent object, you need to pass in a request code as well. The request code is simply an integer value that identifies an activity you are calling. This is needed because when an activity returns a value, you must have a way to identify it. Returning Results from an Intent In order for an activity to return a value to the calling activity, you use an Intent object to send data back via the setData() method: The setResult() method sets a result code (either RESULT_OK or RESULT_CANCELLED) and the data (an Intent object) to be returned back to the calling activity. The finish() method closes the activity and returns control back to the calling activity. Returning Results from an Intent In the calling activity, you need to implement the onActivityResult() method, which is called whenever an activity returns Here, you check for the appropriate request and result codes and display the result that is returned. The returned result is passed in via the data argument and you obtain its details through the getData() method. Returning Results from an Intent Intent Uri getData() setData() Passing Data Using an Intent Object Besides returning data from an activity, it is also common to pass data to an activity. you can use the Intent object to pass the data to the target activity. Add Button to the first activity (main) xml . Passing Data Using an Intent Object Add a TextView and Button to the SecondActivity xml . Passing Data Using an Intent Object .setData(Uri) [Uri.parse()] IntentObject. Passing data with Intent StartActivity() .getData(Uri) .putExtra() Direct IntentObject. StartResultActivity() .getStringExtra() .getIntExtra() getIntent() Bundle putExtras() getExtras() BundleObject. .putString() .putInt() .getString() .getInt() Modify SecondActivity java file to be as follow: Modify SecondActivity java file to be as follow: Modify MainActivity java file to be as follow: Modify MainActivity java file to be as follow: For more details , please go to “How it works” P-67 on your text book .