0

I have an Android app that does about 30 seconds of heavy duty number crunching at the start of the app. How do I display a screen that disappears at the end of the initial processing?

What I currently do is:

My activity_main.xml's topmost view is a ViewAnimator which has (for now) two child views - the main view, and a FrameLayout which currently is a white background that fills the screen and a child text view with "Processing..." on it.

My MainActivity.java's onCreate() method calls a user-defined method doInitialProcessing(); doInitialProcessing calls the ViewAnimator's setDisplayedChild method to show the "Processing" view, then does the number crunching, then calls setDisplayedChild again to show the main view.

In the Eclipse Emulator, what usually happens is, the screen turns white at first, then goes black, then the main view appears. (Every now and then, the "Processing..." text shows up.)

Is this the right way to go about this? Show the initial call to setDisplayedChild be in a separate thread? Does the fact that it is called from onCreate have anything to do with it?

Don Del Grande
  • 411
  • 6
  • 20
  • You should move all heavy work into separate `Thread`, unless you will get `app not responding` message. From this thread, using `Handler` you can change your UI views, for example, to indicate progress of the task. And try to connect real device to computer to make tests easier and more clear. – Alex Salauyou Mar 29 '14 at 03:48

2 Answers2

1

There are two ways to do it:

  • Create a normal activity with a layout for your splash and start the other activity using a timer. (this way, you cannot load data in the second activity)
  • The other way is to create a splash fragment, that is displayed for a few seconds while the content is loaded in the background.

    I use the following timer to start my activity from SPlash activity. This can also be used in a fragment:

     new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent in = new Intent(SplashActivity.this, HomeActivity.class);               
            startActivity(in);
            finish();
        }
    }, SPLASH_TIME_OUT);
    

SPLASH_TIME_OUT is the delay in integer value. (1000 for sec)

amalBit
  • 12,041
  • 6
  • 77
  • 94
0

You can use a Splash Screen that shows something like the logo of your app for instance for some seconds while the app munches on the numbers.Here is a good tutorial on creating Splash Screens.You can also read up on this StackOverflow question.

Community
  • 1
  • 1
Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
  • The problem with a "splash screen" is, it seems to be on for a predetermined length of time. I don't know how long the initial processing will take. – Don Del Grande Mar 29 '14 at 15:39
  • Maybe you should load only the "relevant" data on startup.I don't think it's good design practice to keep the user waiting any longer than 10 seconds let alone 30+..can u tell what you want to load? – Ojonugwa Jude Ochalifu Mar 29 '14 at 15:49