Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

android - How to start an activity in background and show it afterwards?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...