The ingenuity of Android's SplashActivity

As we all know, many of our applications will have a SplashActivity, which is used as the first excessive interface into the application to display a logo information. As shown below, it's mine. Simple weather Plash Activity.



But is it only used to display a logo message? Isn't that too wasteful? The answer is yes.

In fact, when I first learned Android, I thought its function was just to display logo information. But after observing the applications of Sina Weibo and Tencent Weibo, I found that the time of displaying this interface would be different every time. When the network is good, I can not feel it. When the network is bad, I will stay a little longer. So I can say for sure: Here it is. In each Activity, it must have laid the foundation for the emergence of the next Activity, that is to say, some tool classes have been pre-initialized and some necessary data have been loaded. In this way, the next Activity will soon be able to fully display all the data in front of the user to obtain a better user experience.

At this point, maybe some children's shoes will say, I can do these things in Application, and then go directly to MainActivity, in fact, this is also possible, such as mine. Simple weather That's what I did initially, but careful friends will find that every time I enter my Application, the screen will be dark for a period of time, especially the first time. In fact, it's because I put the loading data in the Application, and loading more than 2000 cities takes a certain time. In this way, it will inevitably be dark for a period of time, which makes the user feel less friendly.


OK, that's all. Let's get to the point. Let's take a look at this simple SplashActivity:

/**
 * 
 * @author way 
 * SplashActivity for Preloading Data
 * 
 */
public class SplashActivity extends Activity {
	private static final int SHOW_TIME_MIN = 3000;// Minimum display time
	private long mStartTime;// start time

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Application.CITY_LIST_SCUESS:// If the city list is loaded, send this message
				long loadingTime = System.currentTimeMillis() - mStartTime;// Calculate the total time spent
				if (loadingTime < SHOW_TIME_MIN) {// If it's shorter than the minimum display time, it delays entering MainActivity, otherwise it goes directly into MainActivity.
					mHandler.postDelayed(goToMainActivity, SHOW_TIME_MIN
							- loadingTime);
				} else {
					mHandler.post(goToMainActivity);
				}
				break;
			default:
				break;
			}
		}
	};
	//Enter the Next Activity
	Runnable goToMainActivity = new Runnable() {

		@Override
		public void run() {
			SplashActivity.this.startActivity(new Intent(SplashActivity.this,
					MainActivity.class));
			finish();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		mStartTime = System.currentTimeMillis();//Record the start time.
		Application.getInstance().initData(mHandler);//Start loading data
	}
}

I put the function of loading data in the Application, do some relatively time-consuming things such as copying database files, Reading database city lists, etc. I will not specifically talk about this function of loading data here. Because we can change this function according to different requirements, such as microblog Application replacing asynchronous request for network data and so on. My example is just to provide an extension of ideas, if there are better suggestions or criticisms, you are welcome to leave me a message.






Reproduced in: https://my.oschina.net/cjkall/blog/195781

Keywords: network Android Database less

Added by devsanctum on Thu, 13 Jun 2019 02:05:54 +0300