简体   繁体   中英

onActivityResult in fragment not called

I am trying to attach an image on whatsapp through my app. My app contains two activities A: It contains many fragments B: Just an activity

When I had put an intent filter on my Mainactivity which contains fragments for picking images. So here is what happens:

Tried to attach an picture to whatsapp -> Open Mainactivity(Fragment1) Click on Gridview Item -> Goes to Activity 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. I have to send data back to whatsapp. I have looked into every question related to this problem. But nothing works! :/

Code I am using:

Mainactivity.java(that contains fragments)

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

}

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)

   @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:

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. That's a result code. Change the request code, something like "123" or something, I don't know.

Then, secondly:

  • inside FullImageActivity.class , make sure you build your Intent with the stuff that you need, and then you call

     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. 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); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM