返回上一个片段的正确方法是什么?

时间:2017-03-18 01:28:43

标签: android android-fragments

我有一个MainActivity,其中包含两个片段:一个在列表中显示图像,另一个在列表中显示图像。内置后退按钮适用于从我的DetailFragment导航到我的ImageListFragment,但我还希望在DetailFragment的左上角有一个按钮,允许导航回来到ImageListFragment。我尝试了通常的getActivity().getSupportFragmentManager().popBackStack();,但它没有将我发回ImageListFragment

这是我的MainActivity

  public class MainActivity extends AppCompatActivity implements ImageListFragment.OnGalleryImageSelected {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.root_layout, ImageListFragment.newInstance(), "galleryImageList")
                .commit();
    }
}
@Override
public void OnGalleryImageSelected(int imageResId) {
    final ImageDetailFragment detailsFragment = ImageDetailFragment.newInstance(imageResId);
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.root_layout, detailsFragment, "galleryImageDetails")
            .addToBackStack(null)
            .commit();
}



 }

以下是DetailFragment

  public class ImageDetailFragment extends Fragment {
private static final String IMAGE_ID = "imageResId";

public static ImageDetailFragment newInstance(int imageResId) {

    final Bundle args = new Bundle();
    args.putInt(IMAGE_ID, imageResId);
    final ImageDetailFragment fragment = new ImageDetailFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_image_details, container, false);
    final ImageView imageView = (ImageView) view.findViewById(R.id.gallery_image);
    final Bundle args = getArguments();
    Button button = (Button) view.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            System.out.println("button clicked");
            FragmentManager fm = getActivity().getSupportFragmentManager();
            fm.popBackStack();
        }

    });
    imageView.setImageResource(args.getInt(IMAGE_ID));
    return view;
}

}

这是ImageListFragment

 public class ImageListFragment extends Fragment {
private int[] mImageResIds;
private OnGalleryImageSelected mListener;
public static ImageListFragment newInstance() {
    return new ImageListFragment();
}
@Override
public void onAttach(Context context) {
    super.onAttach(context);

    mListener = (OnGalleryImageSelected) context;

    final Resources resources = context.getResources();
    final TypedArray typedArray = resources.obtainTypedArray(R.array.images);
    final int imageCount = 18;
    mImageResIds = new int[imageCount];
    for (int i = 0; i < imageCount; i++) {
        mImageResIds[i] = typedArray.getResourceId(i, 0);
    }
    typedArray.recycle();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_image_list, container, false);

    final Activity activity = getActivity();
    final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(activity));
    recyclerView.setAdapter(new GalleryImageAdapter(activity));
    return view;
}

class GalleryImageAdapter extends RecyclerView.Adapter<ViewHolder> {

    private LayoutInflater mLayoutInflater;

    public GalleryImageAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        return new ViewHolder(mLayoutInflater
                .inflate(R.layout.recycler_view, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {

        final int imageResId = mImageResIds[position];

        viewHolder.setData(imageResId);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = FooPager.newIntent(getActivity(), position);
                startActivity (intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mImageResIds.length;
    }
}

class ViewHolder extends RecyclerView.ViewHolder {
    private ImageView mImageView;
    private ViewHolder(View itemView) {
        super(itemView);
        mImageView = (ImageView) itemView.findViewById(R.id.gallery_image);

    }
    private void setData(int imageResId) {
        mImageView.setImageResource(imageResId);
    }
}
public interface OnGalleryImageSelected {
    void OnGalleryImageSelected(int imageResId);
}

}

1 个答案:

答案 0 :(得分:2)

试试getActivity().onBackPressed();。它将模拟后退按钮上的按下。我认为这是处理它的最佳方式,因为您希望此按钮的行为与后退按钮完全相同。

这样做可确保如果您稍后对“onBackPressed()”进行更改,这些更改也将应用于您的按钮。