5

Sometimes you have to add a new activity/fragment to an existing application. This activity/fragment could be nested such that in order to access it the user must open the app and navigate through multiple parts of the UI before reaching it.

While developing this new activity/fragment, every time I make a change I have to recompile the whole app, launch it and navigate to the point where the new activity/fragment is shown. This is tedious and slows down the process.

Is there a way to directly launch the activity/fragment currently under development?

ADDENDUM:

Many have suggested to modify existing code or the manifest in order for the app to launch the new activity/fragment first:

Having to modify existing code was exactly what I wanted to avoid. It doesn't sound right to me. So I thought I could write an espresso test for this purpose and directly launch the activity/fragment from it. The problem is espresso keeps the activity/fragment only for the duration of the test so it is visible for a fraction of a second and then disappears.

Marco Romano
  • 1,169
  • 7
  • 24
  • Don't change the launcher in manifest. In your launcher activity after app initialisation launch that activity. – Anurag Singh Feb 17 '17 at 13:34
  • How do you launch the app? Do you export it as apk or directly run/debug it from your IDE? You could add a dummy launch-intent-receiver in your android manifest, so your activity directly gets started when the app is opened. – Tobias G Feb 17 '17 at 13:34

3 Answers3

3

This is what I was able to come up with thanks to @Code-Apprentice answer and this other answer: JUnit - stop it from exiting on finish?.

@RunWith(AndroidJUnit4.class)
public class VanillaActivityTest {

@Rule
public final ActivityTestRule<VanillaActivity> activityTestRule =
        new ActivityTestRule<>(VanillaActivity.class, false, false);

@Test
public void blockingTest() throws Exception {
    Intent intent = new Intent();
    // Add your own intent extras here if applicable.
    activityTestRule.launchActivity(intent);
    CountDownLatch countdown = new CountDownLatch(1);
    countdown.await();
}

}
Community
  • 1
  • 1
Marco Romano
  • 1,169
  • 7
  • 24
1

Probably the best way to do this is to just write instrumented tests. That way, not only do you launch the activity you want, but you also can automate checking how it behaves. You can learn more about automated testing with Getting Started with Testing.

If you want to launch an activity for manual testing, you can use the ActivityTestRule constructor which has the launchActivity flag. If you set this to false, then you have complete control over when the activity is launched and when it is finished. Just launch it manually in your @Before method with launchActivity().

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • That's what I thought (see my addendum) but how to tell espresso to keep my activity/fragment shown instead of tearing it down as soon as the test finishes? – Marco Romano Feb 17 '17 at 13:43
  • @MarcoRomano Ideally, your tests are all automated so that the "disappearing activity" isn't an issue. However, if you need to do some manual testing, see my edit. – Code-Apprentice Feb 17 '17 at 13:47
  • I agree though I think it is not ideal to write automated UI tests to validate a layout design: e.g. do you really want to write UI tests to check that an activity has a particular background color or that a cardview in it has a specifc corner radius? – Marco Romano Feb 17 '17 at 13:52
  • @MarcoRomano Why not? It seems to me that automated tests for layout design will be more accurate than visual inspection. Not to mention more easily repeatable. – Code-Apprentice Feb 17 '17 at 14:03
  • If I was developing a custom view it could make sense to write tests to check that by applying certain settings to the view it shows the required changes on screen. But in regular layout development it seems to me overkill to test that `android:backgroundTint="@android:color/white"` actually yields a white background. – Marco Romano Feb 17 '17 at 14:06
  • 1
    @MarcoRomano It seems to me that a test that verifies the background color can be useful. It is more about ensuring that the background color is not accidentally changed rather than testing that the code actually yields the correct background. Remember that failing tests actually provide more information than passing tests. – Code-Apprentice Feb 17 '17 at 14:09
  • @MarcoRomano Is the purpose of your original question primarily to check the layout of an arbitrary activity? Or did you have other uses in mind? – Code-Apprentice Feb 17 '17 at 14:11
  • Yes it was mainly to check the layout and play with it a bit during development (sorry I mistakenly upvoted one of my comments). – Marco Romano Feb 17 '17 at 14:15
  • 1
    I've tried to use `ActivityTestRule.launchActivity(Intent intent)` in the @Before but still the activity stays visible only for the duration of the test (i.e. a fraction of a second). – Marco Romano Feb 18 '17 at 11:10
0

You can launch a specific Activity on the command line, using adb. The process would be:

  1. Make code changes and build your app (Build not "Run")
  2. Upload the app to the device.
  3. Launch a specific Activity directly: adb shell am start -n com.company.MyApp/.SpecificActivity

More info: How to run a specific Android app using Terminal?

Mr-IDE
  • 7,051
  • 1
  • 53
  • 59