Android Activity 返回时携带参数¶
更新日期 2023-5-19
- 2023-5-19 创建文档
- 2020-4-20 创建文档
我们已经知道启动activity的时候可以传递一些参数。那么在关闭activity时,能不能将一些数据传回上一个activity呢?
本文给出1个例子,实现关闭activity时,将一些数据传回给上一个activity。
例子中会有2个Activity来做示范,ForResultFirstAct
和ForResultSecondAct
。
启动新的Activity¶
ForResultFirstAct
是第一个Activity。启动第二个activity时,要调用startActivityForResult
方法。
private static final int REQ_CODE = 10;
startActivityForResult(new Intent(getApplicationContext(), ForResultSecondAct.class), REQ_CODE);
装载回传数据¶
ForResultSecondAct
是第二个activity。它可以设置返回时携带的数据。
Intent resultIntent = new Intent();
resultIntent.putExtra(K_TITLE, mTitleEt.getText().toString());
resultIntent.putExtra(K_SUB_TITLE, mSubTitleEt.getText().toString());
setResult(RESULT_OK, resultIntent);
finish();
RESULT_OK
是Activity类的静态常量。可用于代表操作的结果。
/** Standard activity result: operation canceled. */
public static final int RESULT_CANCELED = 0;
/** Standard activity result: operation succeeded. */
public static final int RESULT_OK = -1;
/** Start of user-defined activity results. */
public static final int RESULT_FIRST_USER = 1;
获取回传数据¶
ForResultFirstAct
中需要复写方法,获取返回的数据。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE:
if (resultCode == RESULT_OK) {
if (data != null) {
mTitleTv.setText(data.getStringExtra(ForResultSecondAct.K_TITLE));
mSubTitleTv.setText(data.getStringExtra(ForResultSecondAct.K_SUB_TITLE));
}
} else {
Toast.makeText(getApplicationContext(), "未保存修改", Toast.LENGTH_SHORT).show();
}
break;
}
}
startActivityForResult
方法,启动下一个activity。setResult
方法,装载回传数据。onActivityResult
方法,获取回传结果。
requestCode的限制¶
startActivityForResult(Intent intent, int requestCode)
requestCode 有限制;
以下是报错log
com.rustfisher.toturial E/CrashReport: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
at android.support.v4.app.FragmentActivity.checkForValidRequestCode(FragmentActivity.java:806)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
at com.rustfisher.toturial.app.MainActivity.onClick(MainActivity.java:942)
at android.view.View.performClick(View.java:6312)
at android.view.View$PerformClick.run(View.java:24811)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
报出的AndroidRuntimeException
告诉我们,可以考虑启动一个新的task
即设置new task标记 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:765)
at android.app.ContextImpl.startActivity(ContextImpl.java:745)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:331)
本站说明
一起在知识的海洋里呛水吧。广告内容与本站无关。如果喜欢本站内容,欢迎投喂作者,谢谢支持服务器。如有疑问和建议,欢迎在下方评论~