0

I am writing an activity test for an activity we wrote with 3 buttons. 2 of these buttons start other activities.

I can write a test that simulates a button push and then checks if the desired activity is running, but I can't move back from that second activity. The second activity stays at the front and prevents the other tests, that assume the first activity is running, from working properly. They just kind of freeze.

I have a reference to the first activity, but it is the second activity I need to I guess call finish() on. Is there a way to do this?

EDIT: I added some actual source code illustrating my problem in this gist: https://gist.github.com/3076103

It is specifically about testing activities. In the production code everything is fine.

Kurt
  • 4,477
  • 2
  • 26
  • 34

4 Answers4

2

You should probably use http://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor.html to get a reference of the second activity or you can block the second activity from being launched(Still you are guranteed that the call to start the second activity infact had reached till the framework).

Navin Ilavarasan
  • 1,271
  • 11
  • 15
  • +1 This is the correct approach, for code sample, check out [this answer](http://stackoverflow.com/questions/9405561/test-if-a-button-starts-a-new-activity-in-android-junit-pref-without-robotium). – yorkw Jul 12 '12 at 20:59
0

You need a way for your activities to communicate with one another, so that one activity can tell the other to finish. There are several ways you can accomplish this. One method is to create a service within my application; my "second" activities would connect to this service to register a way to receive messages, and my primary activity would connect in order to provide them.

mah
  • 39,056
  • 9
  • 76
  • 93
  • Well in this case the user does something, like pushes a button, or clicks back, and the second activity finishes itself and it goes back to the first. This all works fine in production, the issue is how do I make the second activity finish from the test of the first activity. – Kurt Jul 09 '12 at 11:59
0

In Activity1 add the following to start Activity2

Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);

In Activity2 add the following to start Activity1 and finish Activity2

Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();

For more details: http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/

Ponmalar
  • 6,871
  • 10
  • 50
  • 80
  • Hi all that is working fine. My issue is with the activity test for Activity1. I can verify that it starts Activity2, but Activity2 just stays there preventing the other Activity1 tests from running. – Kurt Jul 09 '12 at 11:57
  • please post your code that will be more helpful to answer you effectively – Ponmalar Jul 09 '12 at 11:59
0
  • If you start the activity using startActivityForResult, then you can close the activity from parent using finishActivity(int requestCode).

    To start the activity :
    startActivityForResult(new Intent(...), 123123 /*requestCode*/);

    And when you want to finish that activity (from caller), use :
    finishActivity(123123 /*requestCode*/)

  • Also there is a way to find, whether child activity is finished or not. But you can track this only when child activity calls finish() for self. To receive the child finish request from the child, you need to override the finishFromChild() method in parent activity.

Karan
  • 12,724
  • 6
  • 40
  • 33
  • My question is about doing it from an activity test. If I have a test for ActivityOne, and I want to test that a button on ActivityOne starts ActivityTwo, that works, but then no other tests can run cos ActivityTwo is sitting there in the foreground. – Kurt Jul 12 '12 at 17:13
  • Before starting the new test, you can call finishAcitivity() for ActivityTwo. – Karan Jul 13 '12 at 03:51