anu saini
anu saini

Reputation: 102

Call onActivityResult of a fragment which is on view pager index of another fragment

I have a MainAcitivity and over it I have fragment A which contains view pager with two fragments B and C.....Frgamnet B is at 0 index of viewpager in fragment A and contains profile section to upload a profile picture from gallery or camera. When a picture is selcted , onActivity result of main activity is called and then I have redirected it to instance of viewpager frgamentA but my problem is how to call onActivityResult of fragment B where main display picture is to be set in an imageview?? MainAcitivity -> frgamentA -> Fragment B

in anticipation of a positive reply..

FRAGMENTB.java......

private void galleryIntent() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);//
    check="file";
    startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
      if (check.equalsIgnoreCase("file"))
            onSelectFromGalleryResult(data);
        else if (check.equalsIgnoreCase("camera"))
            onCaptureImageResult(data);
    }
}

MAIN ACTIVITY.java.

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
    if (f instanceof FragmentA) {    //fragement A  is viewpager fragment
        f.onActivityResult(requestCode, resultCode, intent);
    }
}

FRAGMENTA.java............

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    getTargetFragment().onActivityResult(requestCode, resultCode, intent);
}

HI need to call onActivityResult of FragmentB ??

Upvotes: 2

Views: 806

Answers (4)

Ravi Rajput
Ravi Rajput

Reputation: 539

For this, you'd better call

getParentFragment().startActivityForResult(), 

and in that parent fragment, then, you can implement

 onActivityResult()

and in that method deliver the result to the child fragmen,

  childFragment.getParentFragment().onActivityResult(requestCode, resultCode, data)

Upvotes: 0

Link182
Link182

Reputation: 839

You have to call startActivityForResult() from component which must handle the activityResult.

For example:

if you want to handle the result in Activity myAwesomeActivity:

myAwesomeActivity.startActivityForResult();

if you want to handle the result in Fragment myAwesomeFragment

myAwesomeFragment.startActivityForResult();

Upvotes: 1

Nilesh Panchal
Nilesh Panchal

Reputation: 1069

You need to call startActivityForResult with request Code I have created an example for it's working fine try it.

Activity File

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

public class ViewPagerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);


        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frameContainer, new FragmentA(), "ft");
        transaction.addToBackStack(null);
        transaction.commit();

    }
}

Fragment A

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Objects;

public class FragmentA extends Fragment {


    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_a, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = view.findViewById(R.id.viewPager);
        viewPagerAdapter = new ViewPagerAdapter(Objects.requireNonNull(getActivity()).getSupportFragmentManager());
        viewPager.setAdapter(viewPagerAdapter);

    }

    public class ViewPagerAdapter extends FragmentStatePagerAdapter {
        private int NUM_ITEMS = 2;

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0: // Fragment # 0 - This will show FirstFragment
                    return new FragmentB();
                case 1: // Fragment # 0 - This will show FirstFragment different title
                    return new FragmentC();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Frag B";
                case 1:
                    return "Frag C";
            }
            return "Page " + position;
        }
    }
}

FragmentB

public class FragmentB extends Fragment {


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_b, container, false);
    }
}

And Here is FragmentC where you call startActivityForResult

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class FragmentC extends Fragment {

    Button btnSelect;

    int CAMERA_CODE = 1001;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_c, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        btnSelect = view.findViewById(R.id.btnSelect);


        btnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent cameraI = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraI, CAMERA_CODE);
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_CODE) {
            Log.e("got it", "Frag C Got it.");
        }
    }
}

Upvotes: 0

Pau Molina
Pau Molina

Reputation: 71

If you want call startActivityForResult in fragment you 'll use getActivity() first.

private void galleryIntent() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);//
    check="file";
    getActivity().startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);

}

Upvotes: 0

Related Questions