title: Espresso / How can I move button state between activities in Espresso tests?
body: <p>I have experienced next scenario:</p>
  
  <ul>
  <li>each test in my test-project uses <strong>separate activity</strong></li>
  <li><strong>test1</strong> (main screen) belong to <strong>activity#1</strong>, there is a <strong>button on this screen</strong> which can change state from <strong>STATE#1 (OFF) to STATE#2 (ON)</strong></li>
  <li>tap on button <strong>STATE#1</strong> (from test1) cause raise of another screen which belong to <strong>activity#2</strong> (there is <strong>test2</strong> starting), then on act.#2 user perform some actions which should change button state <strong>to</strong> <strong>STATE#2</strong></li>
  <li>but in my test button <strong>STATE#2</strong> wouldn't be refreshed and passed to the previous the <strong>activity#1</strong></li>
  </ul>
  
  <p><strong>Do I need to sync test data in specific way?</strong> If button's state (which is on <strong>activity#1</strong>) can be changed from another test activity (<strong>activity#2</strong>) </p>
  
  <hr>
  
  <p>Here is example of what I'm doing:</p>
  
  <p>First of all test after which button state should be changed</p>
  
  <p><strong>TEST2 - test that should change button1 state</strong></p>
  
  <pre><code>@RunWith(AndroidJUnit4.class)
   public class ChangeBtnState extends MyIdle
      {
         @Rule
          public ActivityTestRule&lt;Activity#2&gt; EspressoTestRule#2 = new ActivityTestRule&lt;&gt;(Activity#2.class, false, true);
          private MyIdle IdlingRecourseActivity#2;
  
         @Before
          public void SetUpTest2()
          {
           EspressoTestRule#2.getActivity().getSupportFragmentManager().beginTransaction();
  
           IdlingRecourseActivity#2 = (ESP_idling) EspressoTestRule#2.getActivity().getIdlingResource();
           Espresso.registerIdlingResources(IdlingRecourseActivity#2);
          }
         @Test
          public void StartTestRule2()
          {
           // Just some actions on activity#2 after which button1 state should be from STATE#1 to STATE#2
           ViewInteraction ButtonSendtoOFF = Espresso.onView(allOf(ViewMatchers.withId(android.R.id.SomebuttonONact#2))
           .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
           ButtonSendtoOFF.preform(click()); // here click 
          }
  
          }
  
         @After
          public void unregistereSetUpTest2()
          {
              if (IdlingRecourseActivity#2 != null)
              {
                  Espresso.unregisterIdlingResources(IdlingRecourseActivity#2);
              }
          }
  
      }
  </code></pre>
  
  <hr>
  
  <p>Now checking does button state changed on activity#1</p>
  
  <pre><code>@RunWith(AndroidJUnit4.class)
   public class CheckBtnState extends MyIdle
      {
         @Rule
          public ActivityTestRule&lt;Activity#1&gt; EspressoTestRule#1 = new ActivityTestRule&lt;&gt;(Activity#1.class, false, true);
          private MyIdle IdlingRecourseActivity#1;
  
         @Before
          public void SetUpTest1()
          {
           EspressoTestRule#1.getActivity().getSupportFragmentManager().beginTransaction();
  
           IdlingRecourseActivity#1 = (ESP_idling) EspressoTestRule#1.getActivity().getIdlingResource();
           Espresso.registerIdlingResources(IdlingRecourseActivity#1);
          }
         @Test
          public void StartTestRule1()
          {
          // Check some actions FROM activity#2 to change **button1** from STATE#1 to STATE#2
           ViewInteraction ButtonSTATE = Espresso.onView(allOf(ViewMatchers.withId(android.R.id.Button1), withText("OFF"))
           .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
           //ButtonSTATE.preform(click()); // here click 
          }
  
         @After
          public void unregistereSetUpTest1()
          {
              if (IdlingRecourseActivity#1 != null)
              {
                  Espresso.unregisterIdlingResources(IdlingRecourseActivity#1);
              }
          }
  
      }
  </code></pre>
  
  <hr>
  
  <p>BTW, I have look through a lot of topics and even found examples with intents, but it doesn't work for me, perhaps there is should be specific structure.
  If you need extra info please add your question in comments.</p>