TrinityTuts Tips

Get and Set result from activity in android

Learn how we can start our activity for result like we do in camera or we can invoke other application from our application. This is very simple and easy to implement.

Step 1. Create activity in android project or just open activity where we can invoke other activity for result. In simple word just create two activity from one we can launch other on event (or button click) and perform some action in second screen and return some result to first activity.

Now on button click we invoke our second activity like this

Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, 10);

Now on click on button second activity launch when event occur.

Step 2. On second screen when you want to return data to first screen you need to fire intent as shown bellow

Intent setData = new Intent();
setData.putExtra("data", "Hello!");
setResult(RESULT_OK, setData);
finish();

Step 3. Now on your first activity where we receive result

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

    if (requestCode == 10) {
        if(resultCode == RESULT_OK){
            String data =data.getStringExtra("data");
            Log.e("GetResponse", "---"+ data);
        }
    }
}

That’s all hope this will help you. 🙂