StartActivityForResult Android

The result from another activity can be received by using the android startActivityForResult() method, i.e., the information from one activity can also be sent to another activity and is the same for vice-versa, by using the android startActivityForResult() method. A result is needed by the android startActivityForResult method from the second activity, i.e, the activity to be invoked. The onActivityResult method thus needs to be overridden when the second activity returns the result, i.e, it needs to be invoked automatically when the second activity returns the result.

Method Signature:

The two variants of startActivityForResult() method are:

  • public void startActivityForResult (Intent intent, int requestCode)
  • public void startActivityForResult (Intent intent, int requestCode, Bundle options)

Android StartActivityForResult Example:

In the below example, we are demonstrating the usage of the android startActivityForResult method.

activity_main.xml:

In the activity_main.xml file, we will drag a textview and a button from the palette.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity = "center"
    android:layout_height = "match_parent"
    tools:context = ".MainActivity"
    android:orientation = "vertical">
    <TextView
        android:id = "@+id/actionEvent"
        android:textSize = "40sp"
        android:layout_marginTop = "30dp"
        android:layout_width = "wrap_content"
        android:layout_height = "match_parent" />
</LinearLayout>

Activity class:(File: MainActivity.java)

In the MainActivity.java file, we will write the code to invoke another activity and to retrieve the result from that activity.

package com.example.radioapp;
 
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
    TextView actionEvent;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actionEvent = findViewById(R.id.actionEvent);
        actionEvent.setText("Click Here");
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                startActivityForResult(i, 1);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                String returnString = data.getStringExtra("result");
                actionEvent.setText(returnString);
            }
        }
    }
}

SecondActivity class:

To create a new activity follow the below steps:

  • Right-click on the package inside the src
  • Click New
  • Click Other
  • Click Android Activity

In the SecondActivity.java file, we will write the code to display the content of the second activity layout file.

File: SecondActivity.java:

package com.example.radioapp;
 
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
 
public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Intent returnIntent = new Intent();
        returnIntent.putExtra("result","Hello World!!");
        setResult(Activity.RESULT_OK,returnIntent);
        finish();
    }
}

Activity_second.xml:

On creating the above activity, a new XML file will be automatically created. In the second_main.xml file, we will drag an editText, a textView and a button from the palette.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context = ".SecondActivity">
</android.support.constraint.ConstraintLayout>

Android Manifest:(File: AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.radioapp">
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity" >
            <!--Default Intent Filter-->
            <intent-filter>
                <action android:name="android.intent.action.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

Output 1:

Output 2:

 

Content Protection by DMCA.com
Please Share