Android StartActivityForResult example

In this tutorial we will learn the purpose of StartActivityForResult() in android. With the help of this method it is easy to get data from another activity. For getting the result from another activity you need to override the onActivityResult of the first activity.

Let us see a simple example which demonstrates this functionality. In this example there will be two activities, While clicking on Get Message button in first activity , app will go to second activity and it will get the message which is written in second activity on clicking the submit button.

Contents

  1. First activity
  2. Second activity

Implementation of first activity

Use the below code in the layout of your First Activity. This will contain a button to take to second activity and a textview to display result which is provided by the second activity.

activity_first.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv_result_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="16sp"/>


<Button
android:id="@+id/btn_get_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Get Message"
android:textColor="#FFFFFF"
android:background="@color/colorPrimary"/>

</RelativeLayout>

FirstActivity.java

Here we need to implement the onClick for button which needs to be taken into second activity. And there will be OnActivityResult method to receive the result from the second activity

public class FirstActivity extends AppCompatActivity {

    Toolbar toolbar;
    Button btn_get_message;
    TextView tv_result_message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        init();

        btn_get_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivityForResult(intent, 2);
            }
        });
    }


    //method for initialisation

    private void init() {

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        tv_result_message = (TextView) findViewById(R.id.tv_result_message);
        btn_get_message = (Button) findViewById(R.id.btn_get_message);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 2) {
            String message = data.getStringExtra("MESSAGE");
            tv_result_message.setText(message);
        }
    }
}

Second activity

activity_second.xml

Following is the layout for the second activity which will contain an edittext for user to input the content which needs to be transferred into the parent activity and the button to do the respective action.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<EditText
android:id="@+id/ed_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:hint="Enter the message here" />

<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="submit"
android:textColor="#FFFFFF"
android:background="@color/colorPrimary"/>

</RelativeLayout>

SecondActivity.java

This activity will set the result into intent and finish it once the user clicks on the button.

public class SecondActivity extends AppCompatActivity {

EditText ed_message;
Button btn_submit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

ed_message = (EditText) findViewById(R.id.ed_message);
btn_submit = (Button) findViewById(R.id.btn_submit);
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent = new Intent();
intent.putExtra("MESSAGE", ed_message.getText().toString());
setResult(2, intent);
finish();

}
});

}

}

output

startactivityresult

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

Your email address will not be published. Required fields are marked *