onActivityResult() 未在 onBackPressed() 上触发
onActivityResult() not triggered on onBackPressed()
我只是想在按下 "back" 按钮时将对象信息从 Activity2 交换回 Activity1。
一切正常,除了在 Activity1 中 onActivityResult(..)
从未被触发。 因此我无法访问意图信息。有人知道为什么吗?
我检查了这些答案,但它们不起作用:
setResult does not work when BACK button pressed
How to pass data from 2nd activity to 1st activity when pressed back? - android
How to pass the values from one activity to previous activity
Send data to a parent activity onBackPressed
我的代码(如果你需要更多,告诉我):
AndroidManifest.xml:
<activity
android:name="com.example.Activity1"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.Activity2"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait">
</activity>
Activity1.java:
public class MainActivity extends AppCompatActivity {
// other stuff...
private void showActivity2(Test test) {
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("test", test.getSerializableObject());
startActivityForResult(intent, Activity.RESULT_OK);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Test test = data.getExtras().getParcelable("test");
// Do something...
}
}
Activity2.java:
public class Activity2 extends AppCompatActivity {
// other stuff....
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("test", test.getSerializableObject());
setResult(Activity.RESULT_CANCELED, intent);
super.onBackPressed();
}
}
根据 android 中针对 startActivityForResult
的文档
Same as calling startActivityForResult(Intent, int, Bundle) with no
options.
intent Intent: The intent to start.
requestCode int: If >= 0, this code will be returned in
onActivityResult() when the activity exits.
现在你所做的是:startActivityForResult(intent, Activity.RESULT_OK);
其中 Activity.RESULT_OK
实际值是 -1
这就是它不返回结果的原因。
当您为结果启动 activity 然后在您的 onActivityResult
.
中过滤请求代码时,您需要有一个有效的请求代码
您没有在 Activity1.java 上提到请求代码,而是写了 Activity.RESULT_OK 这是一个结果代码。
startActivityForResult(intent, Activity.RESULT_OK);
应该是:
startActivityForResult(intent, REQUEST_CODE);
REQUEST_CODE 应该是最终的静态 int 值。
我只是想在按下 "back" 按钮时将对象信息从 Activity2 交换回 Activity1。
一切正常,除了在 Activity1 中 onActivityResult(..)
从未被触发。 因此我无法访问意图信息。有人知道为什么吗?
我检查了这些答案,但它们不起作用:
setResult does not work when BACK button pressed
How to pass data from 2nd activity to 1st activity when pressed back? - android
How to pass the values from one activity to previous activity
Send data to a parent activity onBackPressed
我的代码(如果你需要更多,告诉我):
AndroidManifest.xml:
<activity
android:name="com.example.Activity1"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.Activity2"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait">
</activity>
Activity1.java:
public class MainActivity extends AppCompatActivity {
// other stuff...
private void showActivity2(Test test) {
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("test", test.getSerializableObject());
startActivityForResult(intent, Activity.RESULT_OK);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Test test = data.getExtras().getParcelable("test");
// Do something...
}
}
Activity2.java:
public class Activity2 extends AppCompatActivity {
// other stuff....
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("test", test.getSerializableObject());
setResult(Activity.RESULT_CANCELED, intent);
super.onBackPressed();
}
}
根据 android 中针对 startActivityForResult
Same as calling startActivityForResult(Intent, int, Bundle) with no options.
intent Intent: The intent to start.
requestCode int: If >= 0, this code will be returned in onActivityResult() when the activity exits.
现在你所做的是:startActivityForResult(intent, Activity.RESULT_OK);
其中 Activity.RESULT_OK
实际值是 -1
这就是它不返回结果的原因。
当您为结果启动 activity 然后在您的 onActivityResult
.
您没有在 Activity1.java 上提到请求代码,而是写了 Activity.RESULT_OK 这是一个结果代码。
startActivityForResult(intent, Activity.RESULT_OK);
应该是:
startActivityForResult(intent, REQUEST_CODE);
REQUEST_CODE 应该是最终的静态 int 值。