0

I have two activities: Main and Splash. My goal is to show Splash activity for 5 secs and then start Main activity. When I start Splash activity on my computer's emulator everything is okay. When the running device is my phone Nexus 5 (plugged to computer) the phone shows a little bit (2 - 3 secs) of Splash activity, then turn into home screen, then (after 2 - 3 secs) starts Main activity. When I install app on my phone with apk file, everything works fine. My Main class doesn't have anything special. I'm using Android Studio if that matters. My Splash screen is just ordinary image. My Splash activity:

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
            Thread timer = new Thread() {
                public void run() {
                    try {
                        sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        startActivity(new Intent("com.example.app.MAIN"));
                    }
                }
            };
            timer.start();
        }

        @Override
        protected void onPause() {
            super.onPause();
            finish();
        }

LogCat:

04-27 16:59:34.655    2502-2502/? I/Crashlytics﹕ Initializing Crashlytics 1.1.10.12
04-27 16:59:34.735    2502-2506/? D/dalvikvm﹕ GC_CONCURRENT freed 316K, 3% free 16924K/17272K, paused 12ms+5ms, total 34ms
04-27 16:59:34.735    2502-2523/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 12ms
04-27 16:59:34.735    2502-2516/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 12ms
04-27 16:59:34.745    2502-2502/? E/OpenUDID﹕ Initialisation isn't done
04-27 16:59:34.775    2502-2506/? D/dalvikvm﹕ GC_CONCURRENT freed 444K, 3% free 16992K/17468K, paused 2ms+2ms, total 20ms
04-27 16:59:34.775    2502-2515/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 4ms
04-27 16:59:34.785    2502-2502/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 5ms
04-27 16:59:34.785    2502-2530/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 5ms
04-27 16:59:34.785      761-809/? I/ActivityManager﹕ Killing 2332:com.android.providers.calendar/u0a1 (adj 15): empty #17
04-27 16:59:34.835    2502-2502/? V/AmazonInsightsSDK﹕ Firing Session Event: _session.start
04-27 16:59:34.835    2502-2502/? I/AmazonInsightsSDK﹕ Amazon Insights SDK(2.1.16.0) initialization successfully completed
04-27 16:59:34.845    2502-2536/? I/AmazonInsightsSDK﹕ Attempting to retrieve variation(s) for project(s):'Sponsored in feed'
04-27 16:59:34.865    2502-2506/? D/dalvikvm﹕ GC_CONCURRENT freed 462K, 3% free 17024K/17516K, paused 3ms+3ms, total 30ms
linkas
  • 175
  • 5
  • 18
  • Is there anything of use in the logger? Could you show us some of what the splash screen looks like? – natewelch_ Apr 27 '14 at 13:38
  • Are you aware the documentation for `sleep` indicates: `The precision is not guaranteed - the Thread may sleep more or less than requested.`? – Justin Jasmann Apr 27 '14 at 13:43
  • @JustinJasmann Correct, but that doesn't explain the behavior. If it's off by a second or two, the new new activity should still be created before the old one is destroyed. – natewelch_ Apr 27 '14 at 13:46
  • Why do you use a `Thread` and the `sleep()` method anyway? You can use a `Timer`, [here](http://developer.android.com/reference/java/util/Timer.html) is some documentation. Or you can is a `Runnable` and run it delayed. – Xaver Kapeller Apr 27 '14 at 14:17
  • Okay, I can use them. But what is the problem with my code? Why it crashes on my phone when it is emulator, and doesn't crash when I install apk file on my phone? – linkas Apr 27 '14 at 14:25
  • So if you install it through android studio onto your nexus 5, it goes back to home before opening the new activity? What about if you disconnect the phone from adb (take out the wire) before starting the app? Does it still have the strange behavior? – natewelch_ Apr 27 '14 at 16:08
  • Well it shows the first activity, then goes to home screen and then shows the second activity. What do you mean by saying disconnect the adb? The activity won't start without emulator.. – linkas Apr 27 '14 at 16:30

1 Answers1

0

You need to call startActivity from the UI thread. An easy way to do that would be to use an AsyncTask instead of creating your own thread directly.

    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            startActivity(new Intent("com.example.app.MAIN"));
        }

    }.execute();
Dave C
  • 928
  • 6
  • 12