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 8745339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:59:29+00:00 2026-06-13T11:59:29+00:00

I have the following Activity: package codeguru.startactivityforresult; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import

  • 0

I have the following Activity:

package codeguru.startactivityforresult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ChildActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.child);

        this.resultButton = (Button) this.findViewById(R.id.result_button);
        this.resultButton.setOnClickListener(onResult);
    }

    private View.OnClickListener onResult = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent result = new Intent();
            result.putExtra(ChildActivity.this.getString(R.string.result), ChildActivity.this.getResources().getInteger(R.integer.result));
            ChildActivity.this.setResult(RESULT_OK, result);
            ChildActivity.this.finish();
        }
    };
    private Button resultButton = null;
}

And the following JUnit test:

package codeguru.startactivityforresult;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.widget.Button;
import junit.framework.Assert;

public class ChildActivityTest extends ActivityInstrumentationTestCase2<ChildActivity> {

    public ChildActivityTest() {
        super(ChildActivity.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();

        this.setActivityInitialTouchMode(false);

        this.activity = this.getActivity();
        this.resultButton = (Button) this.activity.findViewById(R.id.result_button);
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @UiThreadTest
    public void testResultButtonOnClick() {
        Assert.assertTrue(this.resultButton.performClick());
        Assert.fail("How do I check the returned result?");
    }
    private Activity activity;
    private Button resultButton;
}

How do I make sure that clicking the button sets the correct result (with the call to setResult()) that will be returned to any activity which starts this acitivity with startActivityForResult()?

  • 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-06-13T11:59:30+00:00Added an answer on June 13, 2026 at 11:59 am

    With current Activity implementation in the question, i.e. by clicking button in ChildActivity set the result then destroy the activity immediately, there are not much we can do in the ChildActivityTest for testing result related stuff.

    The answer in related question Testing onActivityResult() shows how to unit-test startActivityForResult() and/or onActivityResult() standalone in MainActivityTest. By standalone means MainActivityTest does not depend on ChildActivity’s interaction, instrumentation will capture ChildActivity creation and kill it immediately then return a ready baked mock ActivityResult, hence unit test MainActivity.

    If you don’t want instrumentation interrupt and return the mock ActivityResult, you can let ChildActivity keep going then simulating the interaction in ChildActivity consequently and return the real ActivityResult back to MainActivity. Says if you MainActivity start ChildActivity for result then update a TextView, to test the the whole end-to-end interaction/cooperation, see sample code below:

    public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
      ... ...
    
      public void testStartActivityForResult() {
        MainActivity mainActivity = getActivity();
        assertNotNull(activity);
    
        // Check initial value in TextView:
        TextView text = (TextView) mainActivity.findViewById(com.example.R.id.textview1);
        assertEquals(text.getText(), "default vaule");
    
        try {
          Thread.sleep(2000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
    
        // Create an ActivityMonitor that monitor ChildActivity, do not interrupt, do not return mock result:
        Instrumentation.ActivityMonitor activityMonitor = getInstrumentation().addMonitor(ChildActivity.class.getName(), null , false);
    
        // Simulate a button click in MainActivity that start ChildActivity for result:
        final Button button = (Button) mainActivity.findViewById(com.example.R.id.button1);
        mainActivity.runOnUiThread(new Runnable() {
          public void run() {
            button.performClick();
          }
        });
    
        try {
          Thread.sleep(2000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
    
        getInstrumentation().waitForIdleSync();
        ChildActivity childActivity = (ChildActivity) getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5);
        // ChildActivity is created and gain focus on screen:
        assertNotNull(childActivity);
    
        // Simulate a button click in ChildActivity that set result and finish ChildActivity:
        final Button button2 = (Button) childActivity.findViewById(com.example.R.id.button1);
        childActivity.runOnUiThread(new Runnable() {
          public void run() {
            button2.performClick();
          }
        });
    
        try {
          Thread.sleep(2000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
    
        getInstrumentation().waitForIdleSync();
        // TextView in MainActivity should be changed:
        assertEquals(text.getText(), "default value changed");
      }
    
      ... ...
    }
    

    I add three Thread.sleep() calls here so that you can get a chance see the button clicking simulation when running JUnit Test. As you can see here, a standalone ChildActivityTest is not sufficient to test the whole cooperation, we are actually testing ChildActivity.setResult() indirectly via MainActivityTest, as we need simulate the whole interaction from the very beginning.

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

Sidebar

Related Questions

I have created following activity package com.ali.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import
I have the following activity... package org.dewsworld; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import
Top of my code package com.br.openeed.engtoolspro; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.os.Bundle;
I have the following actvity. package org.dewsworld.ui; import android.app.Activity; import android.os.Bundle; import android.util.Log; import
My code is as follows: package chapter.seven; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import
solved by Emil thx I have following in my main package com.example.surfacetest; import android.os.Bundle;
I have the following code which I am using for an android app: package
I have the following scenario: in activity A, when a user clicks a button,
In my android application, I have following requirement. Activity A --> Activity B(Go to
Within one Activity I have th following piece of code: public void onStartMonitoringToggleClicked(View v)

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.