Mahmud Adam
Mahmud Adam

Reputation: 3579

What is the proper way of returning to previous fragment?

I have a MainActivity that includes two fragments: one that displays images in a list, and another that displays the image from the list that is clicked. The build-in back button works fine for navigating from my DetailFragment back to my ImageListFragment, but I also want to have a button in the top left of the DetailFragment that allows for navigating back to the ImageListFragment. I have tried the usual getActivity().getSupportFragmentManager().popBackStack(); but it does not send me back to the ImageListFragment.

Here is my 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();
}



 }

Here is 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;
}

}

And here is the 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);
}

}

Upvotes: 1

Views: 33

Answers (1)

Feuby
Feuby

Reputation: 718

Try getActivity().onBackPressed();. It will simulate a press on the back button. I think that's the best way to handle it as you want this button to behave exactly like the back button.

Doing so will ensure that if you make changes later to "onBackPressed()" these changes will be applied to your button too.

Upvotes: 2

Related Questions