[英]onActivityResult in fragment not called
I am trying to attach an image on whatsapp through my app. 我正在尝试通过我的应用程序将图像附加在whatsapp上。 My app contains two activities A: It contains many fragments B: Just an activity
我的应用程序包含两个活动A:包含许多片段B:只是一个活动
When I had put an intent filter on my Mainactivity which contains fragments for picking images. 当我在Mainactivity上放置一个意图过滤器时,其中包含用于拾取图像的片段。 So here is what happens:
所以这是发生了什么:
Tried to attach an picture to whatsapp -> Open Mainactivity(Fragment1) Click on Gridview Item -> Goes to Activity B 尝试将图片附加到whatsapp->打开Mainactivity(Fragment1)单击Gridview项->转到活动B
When I click on button in B to send the data back to Whatsapp, it went back to Mainactivity which doesn't even call Fragment's OnActivityResult method. 当我单击B中的按钮以将数据发送回Whatsapp时,它又回到了Mainactivity,它甚至没有调用Fragment的OnActivityResult方法。 I have to send data back to whatsapp.
我必须将数据发送回whatsapp。 I have looked into every question related to this problem.
我已经调查了与此问题有关的每个问题。 But nothing works!
但是什么都行不通! :/
:/
Code I am using: 我正在使用的代码:
Mainactivity.java(that contains fragments) Mainactivity.java(包含片段)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
Fragment1.java Fragment1.java
mGrid.setOnItemClickListener(
new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
// TODO Auto-generated method stub
Intent i = new Intent(getActivity(), FullImageActivity.class);
// passing array index
i.putExtra("im",fileList);
i.putExtra("pos", position);
i.putExtra("folder", folder);
i.putExtra("req", isinint);
startActivityForResult(i, Activity.RESULT_OK);
}
});
Fragment1.java(Onactivityresult) Fragment1.java(Onactivityresult)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK){
Log.i("called","called");
getActivity().setResult(Activity.RESULT_OK, data);
getActivity().finish();
}
}
Activity B: 活动B:
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.i("called","btncalled");
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND, Uri.parse(Environment.getExternalStorageDirectory() + "/Smileys/" + imageUrls[pagerPosition].replace("assets://pics/", "")));
FullImageActivity.this.setResult(Activity.RESULT_OK, shareIntent); //set the file/intent as result
FullImageActivity.this.finish();
}
});
Firstly, don't use Activity.RESULT_OK
as a request code. 首先,不要使用
Activity.RESULT_OK
作为请求代码。 That's a result code. 那是结果代码。 Change the request code, something like "123" or something, I don't know.
更改请求代码,例如“ 123”之类的,我不知道。
Then, secondly: 然后,其次:
inside FullImageActivity.class
, make sure you build your Intent
with the stuff that you need, and then you call 在
FullImageActivity.class
,确保使用所需的内容构建Intent
,然后调用
Intent myIntent = new Intent(); myIntent.putExtra("your_stuff_here"); setResult(Activity.RESULT_OK, myIntent); finish();
so that your code execution returns the result to the fragment that called it. 这样您的代码执行将结果返回给调用它的片段。
inside your caller fragment (Fragment1)
make the call to super
as last thing. 在您的调用方片段
(Fragment1)
,最后调用super
。 Something like this: 像这样:
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub if(requestCode == "123" && resultCode == Activity.RESULT_OK){ Log.i("called","called"); //add your logic here } super.onActivityResult(requestCode, resultCode, data); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.