我有一个MainAcitivity,并且它上面有片段A,其中包含带有两个片段B和C的视图寻呼机..... Frgamnet B在片段A中的viewpager
的0索引处并且包含从库或相机上传配置文件图片的配置文件部分。当一张图片被选中时,主要活动的onActivity结果被调用然后我将它重定向到viewpager
frgamentA的实例但我的问题是如何调用片段B的onActivityResult
,其中主要显示图片将在imageview中设置? MainAcitivity - > frgamentA - >片段B
期待积极的回复..
fragment B.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);
}
}
主要活动.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需要调用FragmentB的onActivityResult
吗?
你必须从必须处理activityResult的组件调用startActivityForResult()
。
例如:
如果你想在Activity myAwesomeActivity
中处理结果:
myAwesomeActivity.startActivityForResult();
如果你想在Fragment myAwesomeFragment
处理结果
myAwesomeFragment.startActivityForResult();
如果你想在片段中调用startActivityForResult
,你将首先使用getActivity()
。
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);
}
您需要调用startActivityForResult
请求代码我已经创建了一个示例,因为它正常工作。
活动文件
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();
}
}
片段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);
}
}
这里是FragmentC,你称之为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.");
}
}
}
为此,你最好打电话
getParentFragment().startActivityForResult(),
然后,在那个父片段中,您可以实现
onActivityResult()
并在该方法中将结果传递给子片段,
childFragment.getParentFragment().onActivityResult(requestCode, resultCode, data)