I have an app that appears to work fine on an android 2.3.5 phone, but crashes on an android 3.0 emulator. I have added numerous Logcat messages to try and spot any difference in behaviour on the two devices. The key code (simplified) where something strange happens is in my splash screen activity here:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
Thread timer;
timer = new Thread()
{
public void run()
{
try
{
sleep(2600);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
finally
{
Intent in;
in = new Intent("android.intent.action.TTMODESELECT");
startActivity(in);
}
}
};
timer.start();
}
protected void onPause()
{
super.onPause();
finish(); // kill the launcher - free its memory
}
Running on the 2.3.5 device I see that it begin then complete the sleep() then start up android.intent.action.TTMODESELECT, but under 3.0 I see it start the sleep – but it never completes, instead the onPause method of my splashscreen class gets called casing a finish() to occur before before the new activity can be started.
Any idea what I can do to fix this?
The system’s allowed to call
onPause()/onResume()any time it likes, I’m afraid. You can’t prevent it from doing this, so you can’t fix it that way.If what you want is for your launcher activity to go away when your real activity appears over the top of it, then it’s perfectly acceptable to do
startActivity(in); finish();to close the launcher once you’ve invoked the real one. Then you just omitonPause()completely.Incidentally, I’m pretty sure you can’t call
startActivity()reliably from an engine thread (finish(), too) — you’ll need to delegate it to your UI thread withActivity.runOnUiThread(). A cleaner solution here is probably to do something like:That avoids the threading issues completely.