Reputation: 129
This question has been raised on multiple threads but their solutions are not working for me.
This is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intent"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="23"/>
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:allowBackup="false">
<activity android:name=".Start">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".Second"/>
</application>
</manifest>
This my Launch Class
package com.example.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class Start extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "First Intent", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, Second.class);
startActivityForResult(intent, RESULT_OK);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this, "After Intent", Toast.LENGTH_LONG).show();
}
}
This is my Second Class
package com.example.intent;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "Second Intent", Toast.LENGTH_LONG).show();
}
}
Now according to the code, the onActivityResult function in Start class should be called when I press the back button on Second Activity class, but its not being called since I am unable to see any Toast stating After Intent.
Please rectify me and provide a solution for this. Thanks.
Upvotes: 0
Views: 270
Reputation: 500
The method onActivityResult isn't being called because you should set the return code in your Second Class.
The Official Documentation says:
When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER.
To set a result code, use the method setResult before the activity is being closed.
Add the following line to your second class:
setResult(RESULT_OK)
For example, you can override the method onBackPressed and put setResult there, like this:
@Override
public void onBackPressed()
{
setResult(RESULT_OK)
super.onBackPressed();
}
Upvotes: 0
Reputation: 249
replace RESULT_OK in startActivityForResult(intent, RESULT_OK); with some custom constant, a positive number. RESULT_OK is -1
Upvotes: 2