using startActivityForResult in android
startActivityForResult is used when you want to start a new activity and get some result back from that new activity. The result will be received only after the new activity finished it’s activity. For example, the main activity starts the second activity by using the method startActivtyForResult, the second activity started then sends back the result to the main activity, the main activity gets the result from the onActivityResult method.
Layout file for MainActivity, activity_main.xml
[code language=”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">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="goToActivity2"
android:text="Go To Activity 2"/>
</RelativeLayout>
[/code]
MainActivity.java
[code language=”java”]
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private static final int RESULT_CODE = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void goToActivity2(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, RESULT_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Log.i("onActivityResult", data.getStringExtra("result"));
}
}
}
[/code]
Layout file for SecondActivity, activity_second.xml
[code language=”xml”]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello"
android:gravity="center"/>
</LinearLayout>
[/code]
Add this to the AndroidManifest.xml file to register this activity.
[code language=”xml”]
<activity android:name=".SecondActivity"/>
[/code]
SecondActivity.java
[code language=”java”]
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent returnIntent = new Intent();
returnIntent.putExtra("result", "Hey, I received your intent!");
setResult(Activity.RESULT_OK, returnIntent);
finish(); // this finish method has to be called in order for the MainActivity to receive the result
}
}
[/code]
Search within Codexpedia
Search the entire web