[英]extra value not passed from activity using onActivityResult to fragment
Update 更新资料
Activity A ==> host MyFragment and from here,user started ==> ActivityB from here I need to get some value from this activty for example value integer and some String to MyFragment that hosted in ActivityA 活动A ==>托管MyFragment,然后从这里开始,用户启动==> ActivityB从这里我需要从此活动中获取一些值,例如,值整数和在ActivityA中托管的MyFragment的一些字符串
I've MyFragment that start the activityB like this : 我有MyFragment这样启动activityB :
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
startActivityForResult(intent,addComment);
and then when user start the activty and finish it I'm trying to pass some value from that ActivityB to MyFragment like this : 然后当用户启动活动并完成活动时,我试图像这样从ActivityB传递一些值到MyFragment :
@Override
public void onBackPressed() {
super.onBackPressed();
//set ok result before finish the activity
Intent returnIntent = new Intent();
returnIntent.putExtra("idBadgeComment", idBadgeComment);
returnIntent.putExtra("totalCommentInserted", totalCommentInserted);
setResult(RESULT_OK, returnIntent);
finish();
}
and I've implement onActivityResult in MyFragment : 并且我在MyFragment中实现了onActivityResult:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "Result from News Comment ");
if(resultCode== Activity.RESULT_OK){
Log.i(TAG, "Result from News OK");
if(requestCode==addComment){
Log.i(TAG, "Result ReqCode Oke");
int idBadgeComment = data.getExtras().getInt("idBadgeComment");
int totalCommentInserted = data.getExtras().getInt("totalCommentInserted");
Log.i(TAG, "idComment: "+idBadgeComment);
Log.i(TAG, "Total Comment Inserted: "+totalCommentInserted);
}
}
}
also I've called onActivityResult in ActivityA that hosted MyFragment : 我也曾在托管MyFragment的 ActivityA中调用onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/**
* call below code to get the result on the fragment
*/
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.viewpager);
fragment.onActivityResult(requestCode, resultCode, data);
}
when I try the code above I only get Log.i(TAG, "Result from News Comment ");
当我尝试上面的代码时,我只会得到
Log.i(TAG, "Result from News Comment ");
and it didn't get the RESULT_OK
value and the Intent data is null, can someone please pointed out where did I go wrong? 并且没有得到
RESULT_OK
值,并且Intent数据为null,请问有人指出我哪里出错了吗? and how to pass the value from activity to fragment ? 以及如何将值从活动传递到片段? or maybe a better way instead of using onActivityResult?
还是比使用onActivityResult更好的方法?
You can try putting super.onBackPressed();
您可以尝试将
super.onBackPressed();
as the last line since RESULT_CANCELED
is returned on back press. 作为自
RESULT_CANCELED
返回以来的最后一行。
Just startActivity by activity. 只需按活动启动活动。
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
getActivity().startActivityForResult(intent,addComment);
Or put your onActivityResult to fragment. 或者将您的onActivityResult片段化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.