Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using android espresso, how to access a view of second activity?

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?

like image 408
Prabin Timsina Avatar asked Oct 18 '22 07:10

Prabin Timsina


1 Answers

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);
like image 119
Prabin Timsina Avatar answered Nov 10 '22 13:11

Prabin Timsina