Showing posts with label Implicit Intent. Show all posts
Showing posts with label Implicit Intent. Show all posts

Friday, August 24, 2012

Passing Data Between Two Activities

In this post we will discuss about how to pass data between two activities. This is very important feature in perspective of the any application that it always needed to pass data between two activities. Here we will use two activities two pass the data first calling activity is called as the parent activity and other is invoked activity child activity.

For simplicity sake, We use an explicit intent for invoking the child activity. For simple invocation without expecting any data back, we use the method startActivity(). However, when we want a result to be returned by the child activity, we need to call it by the method startActivityForResult(). When the child activity finishes with the job, it should set the data in an intent and call the method setResult(resultcode, intent) to return the data through the intent.

When the parent activity expecting result from the invoked activity. The parent activity should have overridden the method onActivityResult(…) in order to be able to get the data and act upon it. 

After the setting up the set activity setResult(...) please call finish() function to return to the parrent activity.

Here we need two methods to impliment on the parrent activity.

  1. startActivtyForResult(..)
  2. onActivityResult(..) 
The child activity must execute its line of codes after then it have to call the following two functions.
  1. setResult(..)
  2. finish()
You can download the working example from here

Here in this example we can see easily our calling activity having two buttons to view He or She. Either selecting one of them HeAcitivty or SheActivity which display a list using listview of selected type of objects. The user can select any of the object then the object will return it to the parent activity with data.


Since we are expecting to get back the selected object, the calling Activity’s code is like this:
     Intent HeIntent = new Intent();                heIntent.setClass(Home.this,HeActivity.class);
startActivityForResult(heIntent,HE_SELECT);
where HE_SELECT is just a constant to help us identify from which child activity the is result obtained, when there is more than 1 child activity, as in this case.

At this point the control is handed over to the BooksActivity. This displays the list of books and the user can scroll through and select a book. When the user selects a book, the selected book needs to be passed back to the CallingActivity. This is how it is done:
      Object o = this.getListAdapter().getItem(position);
      String book = o.toString();
      Intent returnIntent = new Intent();
      returnIntent.putExtra("Selected He",book);
      setResult(RESULT_OK,returnIntent);       
      finish();

The first 2 lines show how to get the selected he from the ListView. Then, you create a new intent object, set the selected book as an extra and pass it back through the setResult(…) method call. The result code is set to RESULT_OK since the job has been successfully done.  After that the finish() method is called to give the control back to the parent activity.

In the parent, the method that gets the control is onActivityResult(…)
protected void onActivityResult(int requestCode, int resultCode, Intent data)
      {
      switch(requestCode) {
      case HE_SELECT:
            if (resultCode == RESULT_OK) {
                String name = data.getStringExtra("Selected He");
                Toast.makeText(this, "You have chosen the He: " + " " + name, Toast.LENGTH_LONG).show();
                break;
            }
      ……….
      }
      }  

Here you notice that the HE_SELECT constant is used to act upon the result. If the result code is RESULT_OK, we take the book selected from the “extra” of the intent that is returned from the child activity. data.getStringExtra("SelectedHe") is called to and the name returned is displayed through a Toast.

Saturday, June 9, 2012

Implicit Intent | Android Tutorial for Beginners



We have seen in the last post about the explicit intents with a very simple example. now we will move to the more interesting concept implicit intents.

This require more theoretical aspects to understand it more clearly.


As we know that, explicit intents need a target activity to execute those type of intents. Here implicit intents does not need the target activity to define over here. I  also said that the android platform resolves as to which component is best suited to respond to an Implicit Intent. How does this happen?

 An Intent object has the following information (among other things like Component name, extras and flags) which is of interest for implicit intents:
  • Action
  • Category
  • Data 

So here the android platform compare these three (action, category and data) to something called "intent filters" are declared by probable target who are willing to accept implicit intents.i.e. 
Intent Filters are the way of any component to advertise its own capabilities to the Android system. This is done declaratively in the AndroidManifest.xml file.

      Note: Intent filters are always define in the android manifest files. Like our first activity which is called first time when application starts.
Some important points in Implicit intents.
  1. Implicit intent never define target components.
  2.  Components willing to receive implicit intent have to declare to handle their specific intent by declaring intent filters.
  3. A component can declare any number of Intent Filters 
  4. There can be more than one component that declares the same Intent Filters.
  5. You can set priority of intent filters to ensure order and responses. 
There are 3 tests conducted in order to match an intent with intent filters:
  1. Action Test
  2. Category Test
  3. Data Test 
 To see how the implicit intent is defined and working please download the code from here.

The ImplicitIntent Activity creates an implicit intent object "contacts". This intent object's component is not set. However, the action is set to "android.content.intent.ACTION_VIEW" and the data's URI is set to "ContactsContract.Contacts.CONTENT_URI". 

Such an intent matches with the intent filter declared by the view contacts native activity.
So, when you run this application, it displays the native UI for viewing the existing contacts on the phone!

Here is the relevant piece of code for the same:
            Button showContacts = (Button) findViewById(R.id.clickhere);
            showContacts.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                Intent contacts = new Intent();
                contacts.setAction(android.content.Intent.ACTION_VIEW);
                contacts.setData(ContactsContract.Contacts.CONTENT_URI);
                startActivity(contacts);
               
            }
            });

In this manner many of the native applications can be seamlessly invoked as one of the activities in our applications through implicit intents.
Here is clearly shown that here no any other target component is defined. here is just a intent filter is set . on click of the button the internal contacts app is going to open.