Android Splash interface allows users to click directly into the main interface

Source: Internet
Author: User

Now casually download an app, Open has a splash interface, why go in there is a splash interface? In fact, users open your product, hope to use the least amount of information to the user the most informative, so that users know what this is a product, this is said products, there is a functional aspects, such as database copy, version update, there is an important is to display the company's logo and so on, Now the mobile Internet is equivalent to the PC side of the product experience more fastidious, we now find a very good product it may not be able to give this company immediately, but the user volume is large, the user to your product recognition, then the company financing is relatively simple, nonsense not much to say


If splash do nothing, it is usually a few seconds into the main interface, the code is as follows:

public class Splashactivity extends Activity {private Handler Handler = new Handler (); @Overrideprotected void OnCreate (Bun Dle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_splash); Handler.postdelayed (New Runnable () {@Overridepublic void run () {Loadui ();}}, 5000);} protected void Loadui () {Intent Intent = new Intent (this,mainactivity.class); startactivity (Intent); Finish ();}}

If this time you give this to the tester, then you are tragic, unless the test is blind or you this bug must be found, we will notice that mainactivity will create 2, the interface will flash, how to solve this bug? We must look at the code to solve the bug , you are very familiar with the business, know what the code is doing, the bug is because the Loadui () method executes two times, and we certainly want him to execute only once, then this involves the judgment, but to see whether this method is encapsulated in a class or this method is separate, If it is a single boolean to make a judgment, and this method is encapsulated in a class, generally based on whether the object of this class is null to judge, if NULL, execution is not NULL, do not execute, this is just a simple logic,

So how do we judge this in our case? We see that the Loadui () method is actually executed in the Runnable interface, that is, the method encapsulated in the Runnable interface, using an anonymous inner class, and now we do not use anonymous objects, directly create objects, and then judge the object, The solution code is as follows:


public class Splashactivity extends Activity {private Handler Handler = new Handler ();p rivate Runnable Runnbale; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_splash) Runnbale = new Runnable () {@Overridepublic void run () {Loadui ();}}; Handler.postdelayed (Runnbale, 5000);} protected void Loadui () {Intent Intent = new Intent (this,mainactivity.class); startactivity (Intent); Finish ();} @Overridepublic boolean ontouchevent (Motionevent event) {if (event.getaction () ==motionevent.action_up) {Intent Intent = new Intent (this,mainactivity.class); startactivity (Intent);//If the Runnable object was previously created, remove the task if (runnbale!= NULL) {handler.removecallbacks (Runnbale);}} Return Super.ontouchevent (event);}}

In some cases we use handler in an interface to perform a runnable task, when the interface jumps, remember to take this runnable to remove from the handler, otherwise it will bring unexpected results, and find a bug is not easy.


There is another way to design the activity of the start mode, we are to start 2 of the same activity, and the activity has a boot mode is singletop, when the activated activity at the top of the stack will not start the activity, Let's set it down and try it,

OK, design this android:launchmode = "singletop" problem can also be solved!



Android Splash interface allows users to click directly into the main interface

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.