7

I've two activities, one is the SplashActivity and the other is MainActivity, an activity containing a webview.

I need to load the webview when showing the splash screen. So I'm looking for a way to load the MainActivity in background in SplashActivity::onCreate();

If intend is directly called, the MainActivity is immediately brought to the front, as well as a stuck in the webview.

I looked up a lot of solutions about splash screen like this . However they don't initialize the MainActivity until the splash time expires.

Community
  • 1
  • 1
SolessChong
  • 3,370
  • 8
  • 40
  • 67
  • You may try using animation by showing the mainactivity late by some milli seconds... – amalBit May 30 '13 at 08:26
  • 3
    You could launch the MainActivity first and start the SplashActivity in onCreate() of MainActivity. After the required duration, you could just close the SplashActivity and MainActivity would reappear so that it would appear as if you have started Main from Splash. – user1721904 May 30 '13 at 08:28
  • @user1721904 Yeah, I think that idea should work. However could you please provide more details on that? Say, should I use `finish()` to stop the SplashActivity? or should I use intend to start it? Also please write it as an answer. THX – SolessChong May 30 '13 at 08:35
  • Posted it as an answer :) – user1721904 May 30 '13 at 08:45
  • 1
    what you can do is instead of having 2 layouts you can have one layout and replace the layout after the time is over – Harsh Dev Chandel May 30 '13 at 09:56
  • Yeah, actually I'm trying out this method right now. @HarshDevChandel – SolessChong May 30 '13 at 11:30

3 Answers3

15

You could launch the MainActivity first and start the SplashActivity in onCreate() of MainActivity. After the required duration, you could just close the SplashActivity and MainActivity would reappear so that it would appear as if you have started Main from Splash.

Let me explain it -

In your MainActiviy use an intent and start SplashActivity by using startActivity and not startActivityForResult as you would not want to pass back the result from SplashActivity to MainActivity.

Now that you are in SplashActivity, start a thread and wait in the thread for the required duration and then call finish() so that SplashActivity will close and the previously started MainActivity comes to the foreground.

Hope this helps.

user1721904
  • 843
  • 4
  • 13
6

Android Apps can cache web data. you can use this to your advantage. (and it worked for me). What I did:

  • I created a WebView in the splash screen.
  • didn't attach it to the UI,
  • requested the webpage I needed in the Main Activity.
  • once the WebView in your splash screen is loaded start the other Activity.

the WebView there will use the cache of the WebView you created in the splash screen.

Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
Mr.Me
  • 9,192
  • 5
  • 39
  • 51
-1

i hope this should work perfectly...

   ProgressDialog pd;   

   pd = ProgressDialog.show(YOUR_ACTIVITY.this,"Loading...", true, false);

            new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub

                    Intent intent = new Intent(YOUR_ACTIVITY.this,
                            NEXT_ACTIVITY.class);

                    startActivity(intent);
                    pd.dismiss();
                }
            }).start();

        }
    });
ashish.n
  • 1,234
  • 14
  • 30