Issue
I am starting from LoginActivity and after logging in, my app goes to next Activity with a RecyclerView. I need to have a reference to the Recycler View. I am doing this but getting null.
RecyclerView recyclerView = (RecyclerView) loginActivity.getActivity().findViewById(R.id.messages_list);
It is null because loginActivity does not contain the RecyclerView. So, my question is, once I move to the second activity, how can I tell espresso to replace loginActivity with the new activity so that I can do this
RecyclerView recyclerView = (RecyclerView) currentActivity.getActivity().findViewById(R.id.messages_list);
If that is not possible, what other ways are there?
Solution
Activity currentActivity;
public Activity getActivityInstance() {
getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
if (resumedActivities.iterator().hasNext()) {
currentActivity = (Activity) resumedActivities.iterator().next();
}
}
});
return currentActivity;
}
RecyclerView recyclerView = (RecyclerView) getActivityInstance().findViewById(R.id.messages_list);
Answered By - Prabin Timsina
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.