@SuppressWarnings("unchecked")
    public final <T extends Activity> T waitForActivity(
            Class<T> activityClass, long timeout, TimeUnit timeUnit) {
        long timeoutInMs = timeUnit.toMillis(timeout);

        Activity mostRecentlyStartedActivity = getMostRecentlyStartedActivity();
        if (mostRecentlyStartedActivity != null
                && mostRecentlyStartedActivity.getClass().equals(
                        activityClass)) {
            return (T) mostRecentlyStartedActivity;
        }

        int startedActivitiesCount = startedActivities.size();
        if (startedActivitiesCount > 0) {
            StartedActivity startedActivity = startedActivities.get(startedActivitiesCount - 1);
            if (startedActivity.getActivity().getClass().equals(activityClass)) {
                return (T) startedActivity.getActivity();
            }
        }

        // we need some kind of container to be shared between two threads
        final List<T> activityContainer = new LinkedList<T>();

        // register the listener, we are now informed when the activity starts
        registerActivityStartListener(activityClass,
                new ActivityStartListener() {
                    public void started(Activity activity) {
                        synchronized (activityContainer) {
                            // OK, the activity has been started. Put its
                            // instance in the container and notify.
                            activityContainer.add((T) activity);
                            activityContainer.notifyAll();
                        }
                    }
                });

        // Now, wait for the activity start by waiting on the container object.
        synchronized (activityContainer) {
            try {
                long time = System.currentTimeMillis();
                while (activityContainer.isEmpty()) {
                    activityContainer.wait(2000);
                    if (System.currentTimeMillis() - time > timeoutInMs) {
                        return null;
                    }
                }
            }
            catch (InterruptedException e) {
            }

            if (activityContainer.size() > 0) {
                return activityContainer.get(0);
            }
        }

        /*
         * Container was empty, we were either interrupted or more likely, the
         * timeout was reached. Return null.
         */
        return null;
    }
@SuppressWarnings("unchecked")
    public final <T extends Activity> T waitForActivity(
            Class<T> activityClass, long timeout, TimeUnit timeUnit) {
        long timeoutInMs = timeUnit.toMillis(timeout);

        Activity mostRecentlyStartedActivity = getMostRecentlyStartedActivity();
        if (mostRecentlyStartedActivity != null
                && mostRecentlyStartedActivity.getClass().equals(
                        activityClass)) {
            return (T) mostRecentlyStartedActivity;
        }

        int startedActivitiesCount = startedActivities.size();
        if (startedActivitiesCount > 0) {
            StartedActivity startedActivity = startedActivities.get(startedActivitiesCount - 1);
            if (startedActivity.getActivity().getClass().equals(activityClass)) {
                return (T) startedActivity.getActivity();
            }
        }

        
        final List<T> activityContainer = new LinkedList<T>();

        
        registerActivityStartListener(activityClass,
                new ActivityStartListener() {
                    public void started(Activity activity) {
                        synchronized (activityContainer) {
                            
                            
                            activityContainer.add((T) activity);
                            activityContainer.notifyAll();
                        }
                    }
                });

        
        synchronized (activityContainer) {
            try {
                long time = System.currentTimeMillis();
                while (activityContainer.isEmpty()) {
                    activityContainer.wait(2000);
                    if (System.currentTimeMillis() - time > timeoutInMs) {
                        return null;
                    }
                }
            }
            catch (InterruptedException e) {
            }

            if (activityContainer.size() > 0) {
                return activityContainer.get(0);
            }
        }

        
        return null;
    }
@ SuppressWarnings ( "unchecked" ) public final < T extends Activity > T waitForActivity ( Class < T > activityClass , long timeout , TimeUnit timeUnit ) { long timeoutInMs = timeUnit . toMillis ( timeout ) ; Activity mostRecentlyStartedActivity = getMostRecentlyStartedActivity ( ) ; if ( mostRecentlyStartedActivity != null && mostRecentlyStartedActivity . getClass ( ) . equals ( activityClass ) ) { return ( T ) mostRecentlyStartedActivity ; } int startedActivitiesCount = startedActivities . size ( ) ; if ( startedActivitiesCount > 0 ) { StartedActivity startedActivity = startedActivities . get ( startedActivitiesCount - 1 ) ; if ( startedActivity . getActivity ( ) . getClass ( ) . equals ( activityClass ) ) { return ( T ) startedActivity . getActivity ( ) ; } } // we need some kind of container to be shared between two threads final List < T > activityContainer = new LinkedList < T > ( ) ; // register the listener, we are now informed when the activity starts registerActivityStartListener ( activityClass , new ActivityStartListener ( ) { public void started ( Activity activity ) { synchronized ( activityContainer ) { // OK, the activity has been started. Put its // instance in the container and notify. activityContainer . add ( ( T ) activity ) ; activityContainer . notifyAll ( ) ; } } } ) ; // Now, wait for the activity start by waiting on the container object. synchronized ( activityContainer ) { try { long time = System . currentTimeMillis ( ) ; while ( activityContainer . isEmpty ( ) ) { activityContainer . wait ( 2000 ) ; if ( System . currentTimeMillis ( ) - time > timeoutInMs ) { return null ; } } } catch ( InterruptedException e ) { } if ( activityContainer . size ( ) > 0 ) { return activityContainer . get ( 0 ) ; } } /*
         * Container was empty, we were either interrupted or more likely, the
         * timeout was reached. Return null.
         */ return null ; }
Blocks until an {@link Activity} of the given type is started. The
instance of the started activity is then returned. If such an activity is
not started within the given amount of milliseconds, returns null.

@param activityClass
the type of activity to wait for
@param timeout
amount to wait for activity start
@param timeUnit
the unit of the timeout parameter
@return the activity waited for, or null if timeout was reached before
any suitable activity was started
Blocks until an { @link Activity } of the given type is started . The instance of the started activity is then returned . If such an activity is not started within the given amount of milliseconds returns null .