Showing posts with label android tutorial. Show all posts
Showing posts with label android tutorial. 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 23, 2012

Pre-Packaged Applications | Android Tutorial for Beginners

There are many applications that are pre-packaged in the android platform that can be reused by the other custom applications. As we have stated in the previous  part 3. Implicit intents there we have provided the example of these types of one application related to contact application of android.


In this part of tutorial we will explore that how we can use the pre-packaged applications using impliocit intents. In these types of application we will just call the pre-packaged application using intents and pass them to the activity and after then we will start our activity with the startActivity() method. However, for each of these intents, the platform find the most befitting activity and invoke the same:

1. Call a number(Dial a number via application)
      Intent callNumber = new Intent();
      callNumber.setAction(android.content.Intent.ACTION_CALL);
      callNumber.setData(Uri.parse("tel:123456789"));
startActivity(callNumber);

This will call the number 123456789.If you want to call any number you want then you have to just add a edit text control then pass the desired value to the Uri.parse() method then it will dial your specified number.


 
2. Browse the web for a given url:

      Intent searchText = new Intent(Intent.ACTION_WEB_SEARCH);
      searchText.putExtra(SearchManager.QUERY, "Android Demo);
      startActivity(searchText);

This will start the google search  engine to search the specified string "Android Demo" and the get back the result on the screen. here the same thing as above specified in the dialed number application you can define a edit text control the pass the desired string to the appropiate space in the putExtra() method then the same result will show on the screen.


3.  View google maps for a given location(find out any location)

      Intent searchLocation = new Intent(Intent.ACTION_VIEW, 
      Uri.parse("geo:0,0?q=India"));
startActivity(searchLocation);

This will show the "India" location on the Google map. You can repeat the smae method for the edit text to search your desired location.


4. View Contacts on the phone(Navigate to view contacts)

Intent contacts = new Intent();
      contacts.setAction(android.content.Intent.ACTION_VIEW);
      contacts.setData(ContactsContract.Contacts.CONTENT_URI);
      startActivity(contacts);


I have created an Android eclipse project which showcases all of these examples by taking inputs from the end user. You can access the same here.