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);
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);

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();

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.

Monday, August 20, 2012

Simple steps to make android development enviroment

Simple steps to make android development enviroment

1): First of all go to you browser and then search "Android system Requirement".
     Because it will tell you which software version you needed actually.
     Please follow the following link:

2): Download latest JDK with JRE (JDK 7 preferably )
      Please use the following ink to download the latest JDK version:

3): Download android SDK (Software development kit)
     You must have to download the android software development kit
      use the following link to download it:

     a): Now install sofware
     b): Now install Android-platform tools (from server)
     c): Now install android SDK tools (from server)
     d): Dowonload specific required android API as you required to install

    After installation you will see the following image

4):  Now Download eclipse(Please check eclipse as per system
requirement ) version is most important thing so please check the
specific version. Please download eclipse 4.2

5): After downloading eclipse now Open eclipse as administrator and
then do following steps.

    a): Go to Help => Install New S/W
    b): Download ADT Plugin from server android.
    c): Click on "Add" Button
         Put the name "ADT Plugin"
         Now target (browse) your adt plugin zip file dont needed to
unzip the file
         Now click 'ok'
    d): Just 'check' the sdk Android option do not check the "NDK"  checkbox.
    e): follow the steps and finish.

6): Now your eclipse with restart now start working with android development.