The trick of the SplashActivity of Android

As we all know, many of our applications will have a SplashActivity, used as the first over-enter the application interface, display a logo information. As shown below, it is my simple weather of SplashActivity.



However, its role is just to display a logo information it? Is this not too wasteful? The answer is yes.

In fact, the beginning of time to learn Android, and I also think that its role is only used to display the logo information, but after observing Sina Weibo, Tencent Weibo and other applications, I found that every time the display this interface will be different, network do not feel it is good, bad network when the time will stay a little longer, so I can say for sure: in this Activity inside, certainly lay the foundation for the emergence of the next Activity, that is pre-initialized Some tools, load the necessary data. In this case the next Activity will soon be able to complete all the data displayed in front of the user, get a better user experience.

Here, perhaps some would say the children's shoes, I can do these things in the Application ah, then go directly to MainActivity, in fact, this is also possible, such as my simple weather initially did just that, but attentive friends will find that every time I enter the application when the screen black for some time, especially for the first time into the more obvious, in fact, because I put things on the application of loading data inside, and load more than 2,000 cities will take some Time. This will inevitably black for some time, so feel to the user and the not so pro.


OK, paving the way to get here, here cut to the chase, let's look at this simple SplashActivity:

/**
 * 
 * @author way 
 * 预先加载数据的SplashActivity
 * 
 */
public class SplashActivity extends Activity {
	private static final int SHOW_TIME_MIN = 3000;// 最小显示时间
	private long mStartTime;// 开始时间

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Application.CITY_LIST_SCUESS:// 如果城市列表加载完毕,就发送此消息
				long loadingTime = System.currentTimeMillis() - mStartTime;// 计算一下总共花费的时间
				if (loadingTime < SHOW_TIME_MIN) {// 如果比最小显示时间还短,就延时进入MainActivity,否则直接进入
					mHandler.postDelayed(goToMainActivity, SHOW_TIME_MIN
							- loadingTime);
				} else {
					mHandler.post(goToMainActivity);
				}
				break;
			default:
				break;
			}
		}
	};
	//进入下一个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();//记录开始时间,
		Application.getInstance().initData(mHandler);//开始加载数据
	}
}

I have here the function of loading data on Application inside, do some copy the database file, read some of the relatively time-consuming things such as database list of cities, where not specifically say this function to load the data. Because we can change the function according to different needs, such as micro-blog application to request the network to replace asynchronous data. I offer this example is only an extension of the idea, if you have better suggestions or criticisms are welcome to give me a message.






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

Guess you like

Origin blog.csdn.net/weixin_34384915/article/details/91756535