Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7542709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:14:19+00:00 2026-05-30T08:14:19+00:00

I have an activity which preforms some work in the background and based on

  • 0

I have an activity which preforms some work in the background and based on the result of this work starts one of two other activities. How can I write a unit test to validate its behavior?

I tried to use ActivityUnitTestCase but it blows up on an attempt to show a progress dialog. With ActivityInstrumentationTestCase2 I could not find any way to intercept activity destruction. Any word of advice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:14:21+00:00Added an answer on May 30, 2026 at 8:14 am

    ActivityInstrumentationTestCase2 is the correct approach as the other class is deprecated. To test what has happened after your main Activity, let’s call it the ProgressActivity, you should use an ActivityMonitor. I think you want to intercept the Activity creation, not the destruction.

    I’m assuming here that ProgressActivity starts another Activity (let’s say A1, A2, or A3) after some calculation is done in the background.

    Your test case should be something like this:

    public static final HashSet<Class<? extends Activity>> TARGET_ACTIVITIES = new HashSet<Class<? extends Activity>>();
    static {
        TARGET_ACTIVITIES.add(A1.class);
        TARGET_ACTIVITIES.add(A2.class);
        TARGET_ACTIVITIES.add(A3.class);
    }
    
    private static final int TIMEOUT = 7000;
    
    public void testRandomActivityStarted() {
        @SuppressWarnings("unused")
        ProgressActivity activity = getActivity();
        final Instrumentation inst = getInstrumentation();
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
        intentFilter.addCategory("MY_CATEGORY");
        ActivityMonitor monitor = inst.addMonitor(intentFilter, null, false);
        // Wait, before the Activity started
        monitor.waitForActivityWithTimeout(TIMEOUT);
        assertEquals(1, monitor.getHits());
        Activity randomActivity = monitor.getLastActivity();
        Log.d(TAG, "monitor=" + monitor + "   activity=" + randomActivity);
        // Unfortunately, it seems randomActivity is always null even after a match
        if ( randomActivity != null ) {
            assertTrue(TARGET_ACTIVITIES.contains(randomActivity.getClass()));
        }
        inst.removeMonitor(monitor);
    }
    

    The trick here is to use a category in the IntentFilter, because if you rely on getLastActivity() you may be disappointed as it seems it’s always null. To be able to match this category you should use it when you start A1, A2 or A3 (Intent.addCatrgory())

    This example was adapted from the one that illustrates ActivityMonitor in Android Application Testing Guide.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Activity which is basically a big webview with some other buttons.
I have an activity which is called by a few other activities. For example,
I have an activity which has some text views and other data to be
I have an activity which shows some List entries. When I click on a
I have activity A that starts activity B which starts activity C: A ->
Assume I have an Activity which contains two FrameLayouts (let's call them FrameA and
I have 2 activities, the first one is the startup one which in it's
I have an activity which have a method in it. In this method I
this is the current state/situation: I have an Activity which binds a Service which
I have one activity which download data from the web. In middle of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.