Search code examples
androidandroid-fragmentsandroid-activityandroid-recyclerview

How to move from fragment to activity?


I am stuck in a situation where I have to move from recycler view fragment to an activity but doesn't find a working solution for that.please help Thanks


Solution

  • Basically, you just need a Context instance to open any Activity: context.startActivity(new Intent(context, SomeActivity.class)

    I assume you are asking about opening new Activity depending on which RecyclerView item was clicked. In that case, you should add View.OnClickListener to the ViewHolder.

    The easiest way is to handle click inside ViewHolder. Context instance is accessible from any View using the View.getContext() method. However, I prefer to delegate click back to Fragment/Activity and then open a new screen from there. Check out the sample here: https://github.com/IvanShafran/abbyy-mobile-school-2019/tree/master/RecyclerViewSample