Friday, June 10, 2011

How To Use startActivityForResult method in android

In the previous discussion we got some information about how to use Intent class in the android. Mainly Intent class is used for moving from one frame to another frame or we can say like this also Intent class is used for moving from one screen to another screen in the same application or some times navigate to other application screen also.Ok now what is the need for startActivityForResult() method in the application?

                       Intent's are used to move forward from one screen to another screen and after moving forward if we want to move backward then we have to use startActivityForResult(....) method.In the following example i will show u how to use startActivityForResult(..) method along with Intent class in the application.

Try to create a new application in eclipse and here i am creating the application with the name StartActivityForResultExample .

//CurrentActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CurrentActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
          
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
                startActivityForResult(intent, 0);
              
            }
        });
    }
}


So in the above java file observe the code in onClick(...) method.There the code u have to observe is like this

Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
startActivityForResult(intent, 0);

As u know Intent class is used to navigate from one activity to another activity and the next line is startActivityForResult(intent,0) here 'intent' object is given as one parameter and another parameter here is '0' here why i am giving zero is here we can give any number .So that number is treated as id for that particular intent because if multiple intent's are used the based on this id we can find the which intent is active now.

In the next step i am writing the main.xml(res/layout/main.xml)
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    >

<Button android:text="Next"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        ></Button>
</LinearLayout>

Here in main.xml i am just creating one button so when we click on this button then we can move to next activity.

In the next step we write the code for NextActivity.java
//NextActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class NextActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nextactivitymain);
        Button button2=(Button)findViewById(R.id.button2);
     
        button2.setOnClickListener(new OnClickListener() {
           
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent();
                setResult(RESULT_OK, intent);
                finish();
               
            }
        });
      
    }

}

Please observe the code in onClick(...) method here i am creating one intent with out any parameters and giving the intent object to setResult(...) method if the result is acceptable then we give the parameter as RESULT_OK and after this finish() method get executed because the NextActivity class knows because of which intent this NextActivity is activated so it goes back to CurrentActivity class.

For the NextActivity class i am creating new xml file and it is created in res/layout folder and its name is nextactivitymain.xml
//nextactivitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  >
    <Button android:text="Back" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
   
</LinearLayout>
Please observe the code once again if u have any doubts the please comment to this tutorial

Note::Don't forget to give the NextActivity class information in AndroidManiFest.xml file.
<activity android:name=".NextActivity" ></activity>







7 comments:

  1. i have written as it is in my android application but a am getting error in the emulator

    application has stopped unexpectedly

    ReplyDelete
  2. Pls check u have added the tag

    in AndroidManiFest.xml file.

    ReplyDelete
  3. check the setContentView(...) is correct or not in NextActivity class

    ReplyDelete
  4. Hi,

    I am inserting data to SqliteDB when i click on submit button of activity2 it is stored successfully in Databse and it calls activity1.In activity1 i need to display date as listview which is retriving from activity2.


    Thanks,
    Murali.
    9041309281

    ReplyDelete
  5. hi !! i'am passing a data to the main-activity from the child activity i have a overloaded method on Activity-result were i could extract the data of child activity but i am not able to receive.can i send data to main activity??

    ReplyDelete
  6. But where and how do I store data to pass? Have I missed something?

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete