shutting down android sub activity
<p>I'm trying to implement a sub-activity to play an mp4 movie using MediaPlayer. In trying to finish the activity with setResult and finish inside of onCompletion proved problematic. Then I started searching and searching for examples of closing a sub activity and learned that it must finish from the UI thread.</p>

<p>I simplified the sub activity removing most the media player and moved the finish code into onCreate to at least get it to work from there. </p>

<p>This works:</p>

<pre><code>    // calls onActivityResult, graceful exit, etc
    Log.d("TAG", Thread.currentThread().getName()); // outputs "main"
    signMovieActivity.this.setResult(99);
    signMovieActivity.this.finish(); 
</code></pre>

<p>So, how do I do this from onCompletion? The best suggestion I've found so far is to use a handler to do it however this code inside of onCreate works just as poorly as calling finish from the mediaplayer callback:</p>

<pre><code>    Handler handler = new Handler();
    handler.post(new Runnable() {
        public void run() {
        Log.d("TAG", Thread.currentThread().getName()); // outputs "main"
            signMovieActivity.this.setResult(99);
            signMovieActivity.this.finish(); // Full meltdown starting with "I/AndroidRuntime(26581): AndroidRuntime onExit calling exit(0)" 
        }
    });
</code></pre>

<p>Please put me back on track!</p>

<p>Here is logcat output when finish() is at the end of onCompletion():</p>

<pre><code>D/movie( 2144): onCompletion called
D/movie( 2144): thread is main
I/FFPlayer( 1169): tm 05460 [InputDemux] Start to wait a/v done 2 2
I/FFPlayer( 1169): tm 00858 [InputDemux] Start to wait a/v done 2 2
I/FFPlayer( 1169): tm 01216 [AudioProc] thread exited
I/FFPlayer( 1169): tm 00902 [InputDemux] Start to wait a/v done 0 2
I/FFPlayer( 1169): tm 300655 [VideoPlay] thread exited
I/FFPlayer( 1169): tm 00473 [VideoProc] thread exited
I/FFPlayer( 1169): tm 03737 [inputDemux] thread exited.
I/FFPlayer( 1169): tm 85592 FFPlayer deleted, mEngine = 0x3c148 this = 0xc860, Count = 0
D/dalvikvm( 2026): GC_EXPLICIT freed 823 objects / 57624 bytes in 55ms
D/dalvikvm( 2037): GC_EXPLICIT freed 618 objects / 42128 bytes in 71ms
&lt;&lt;&lt; finish called here &gt;&gt;&gt;
I/AndroidRuntime( 2144): AndroidRuntime onExit calling exit(0)
I/ActivityManager( 1357): Process com.sign (pid 2144) has died.
I/WindowManagerService( 1357): WIN DEATH: Window{4617dc40 com.sign/com.sign.signActivity paused=false}
I/ActivityManager( 1357): Start proc com.sign for activity com.sign/.signActivity: pid=2165 uid=10126 gids={3003, 1015}
I/WindowManagerService( 1357): WIN DEATH: Window{461a3f90 com.sign/com.sign.signMovieActivity paused=true}
I/WindowManagerService( 1357): WIN DEATH: Window{460f5a10 SurfaceView paused=false}
&lt;&lt;&lt; entire application restarts &gt;&gt;&gt;
</code></pre>
