Android Development Notes: Implementation of Splash

Source: Internet
Author: User

What is Splash?
Splash is a screen that starts before the application starts. The above section briefly describes the vendor, manufacturer's LOGO, name, version, and other information of the application. Most of the information is a picture, the message is automatically displayed after several seconds, and the application's main page is displayed. On PCs, applications on various platforms are common. Most of the applications are displayed in the center of the screen, such as Microsoft Office series or GIMP. Splash is the most common in a variety of Games. Almost all games start with a full screen image, which usually displays the manufacturer's LOGO and game name. On mobile devices such as mobile phone tablets, Splash similar to PCs is rare. At least for Android and iOS, native applications do not have such Splash, but I do not know when to start, this kind of Splash has become popular among third-party applications, and almost all third-party applications start Splash. These Splash features full screen, LOGO, vendor name, application name version, etc. After about 3 to 5 seconds, the Splash automatically disappears and is displayed on the application homepage. Many applications also display the loading process on the Splash page.
The following describes how to implement Splash in Android and its advantages and disadvantages:
Use Activity as Splash
This may also be the most common method. The method is to use an Activity to set a background for it or display information (vendor, LOGO, name, and version ), display it for several seconds, finish (), and start the main Activity of the application. Copy codeThe Code is as follows: <activity android: name = ". SplashActivity"
Android: theme = "@ android: style/Theme. NoTitleBar. Fullscreen"
Android: noHistory = "true"
Android: configChanges = "orientation | keyboardHidden"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>

Copy codeThe Code is as follows: public class SplashActivity extends Activity {
Private Handler mMainHandler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
Intent intent = new Intent (Intent. ACTION_MAIN );
Intent. setClass (getApplication (), NotTomorrowActivity. class );
Intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
StartActivity (intent );
// OverridePendingTransition must be called AFTER finish () or startActivity, or it won't work.
OverridePendingTransition (R. anim. activity_in, R. anim. splash_out );
}
};

@ Override
Public void onCreate (Bundle icicle ){
Super. onCreate (icicle );
GetWindow (). setBackgroundDrawableResource (R. drawable. kg );
MMainHandler. sendEmptyMessageDelayed (0, 5000 );
}

// Much easier to handle key events
@ Override
Public void onBackPressed (){
}
}

The advantages of using Activity are:
Easy control of KEY events
When displaying Splash, you should not respond to events, such as touch events, BACK events, or menus. Because this is a separate Activity, you can easily disable these user events, you don't have to worry about influencing other logics, or worry about re-enabling them in time.
Easy to customize
Because it is a separate Activity, you can set it to full screen, without TitleBar, or other styles and styles. In actual use, almost all Splash is set to full screen. Because it is a separate Activity, it does not matter even if your application is not full screen, because setting SplashActivity to full screen will not affect other activities.
The logic is separated from the subject logic, which is easy to maintain
For example, in the first two points, because it is a separate Activity, all the Splash-related logic is in it, and it is separated from the Activity of the application subject, therefore, the logic in the Splash does not affect other activities, which is easier to modify and maintain because it will not affect each other together.
The only drawback of using Activity is that it cannot use the time displayed by Splash for data loading. Because it is a separate Activity, other activities cannot be controlled, and other activities have not yet been created.
Use ViewSwitcher as the Splash
This can also be used as Splash. ViewSwitcher is a ViewGroup that has two sub-views and can only display one of them at a time. The main practice is to set the RootView of the Activity to ViewSwitcher, and use a layout (such as ImageView) as the Splash as the first sub-View of ViewSwitcher; then, use the main layout of the Activity as the second sub-View; when the Activity is started, the image view as the Splash is displayed first. After a few seconds, the main layout is displayed. In fact, ViewSwitcher is usually used to load data in the Activity. a progress bar is displayed first, and a real layout is displayed when there is data.
Advantages of ViewSwitcher
The advantage of ViewSwitcher is that you can use the Splash time to load data, so that users do not have to wait for the Splash before loading data.Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<ViewSwitcher xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/view_container"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: inAnimation = "@ anim/activity_in"
Android: outAnimation = "@ anim/splash_out">
<ImageView android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: src = "@ drawable/kg"
Android: scaleType = "fitXY"/>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView android: id = "@ + id/header"
Style = "@ style/header_text"/>
<TextView android: id = "@ + id/header_tip"
Style = "@ style/task_text"/>
<ListView android: id = "@ + id/task_list"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: footerDividersEnabled = "true"
Android: background = "@ color/white"/>
</LinearLayout>
</ViewSwitcher>

Copy codeThe Code is as follows: public class NotTomorrowActivity extends Activity {
Protected static final String TAG = "NoTomorrowActivity ";
Protected static final int MSG_SHOW_LAYOUT = 10;
Private static final int MENU_ADD_TASK = 0;
Private Handler mMainHandler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
Switch (msg. what ){
Case MSG_SHOW_LAYOUT:
Final ViewSwitcher container = (ViewSwitcher) findViewById (R. id. view_container );
Container. showNext ();
ImageView view = (ImageView) container. getChildAt (0 );
View. setImageResource (0 );
Container. removeViewAt (0 );
MSplashing = false;
Break;
Default:
Break;
}
}
};
Private boolean mSplashing;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
MSplashing = true;
SetContentView (R. layout. not_tomorrow_activity );
MMainHandler. sendEmptyMessageDelayed (MSG_SHOW_LAYOUT, 5000 );
}
@ Override
Public void onBackPressed (){
If (! MSplashing ){
Super. onBackPressed ();
}
}

@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
If (mSplashing ){
Return false;
}
Menu. add (0, MENU_ADD_TASK, 0, R. string. add_hint );
Return super. onCreateOptionsMenu (menu );
}

@ Override
Public boolean onPrepareOptionsMenu (Menu menu ){
If (mSplashing ){
Return false;
}
Return super. onPrepareOptionsMenu (menu );
}
}

Disadvantages of ViewSwitcher
Pay attention to the event
You must disable events such as BACK and MENU when displaying Splash, and enable the events again after the Splash ends.
No way to full screen
Unless the subject Activity is in full screen mode, you cannot set the View to full screen mode and then set it back.
Logic twisted together, difficult to maintain
Just like the preceding prohibition and enable event, all these things are in the Activity, and all the logic is in the Activity, which is naturally difficult to maintain and modify and prone to errors.
Recommended Practices
We recommend that you do not use Splash, or use it for the first time after the application is installed. It is meaningless from the user's point of view, so you can see that there are no Splash or other things in the Android or iOS native applications. The application should be directed to the topic so that the user can immediately access the page he or she cares about most. Similarly, application usage tips are useless. The really good ones should be simple and easy to use, instead of making a lot of tutorials or tips. Instead of spending time on Splash or tips, consider how to simplify the operation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.