The Android Splash interface allows you to click to access the main interface.

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/lmj623565791/article/details/23613403

Most apps now have the Splash interface. The following lists the functions of the Splash page:

1. display the logo to improve the company image

2. initialize data (copy data to SD)

3. Improve user experience

4. Check whether the connection server has a new version.

However, if the Splash page does not perform any operations, I prefer to provide a user who clicks the Splash interface to directly access the main interface.

Generally, our SplashActivity writes the following:

package com.example.testsplashdemo;import android.os.Bundle;import android.os.Handler;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.MotionEvent;public class SplashActivity extends Activity{private Handler handler = new Handler();@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);handler.postDelayed(new Runnable(){@Overridepublic void run(){Intent intent = new Intent(SplashActivity.this, MainActivity.class);startActivity(intent);}}, 3000);}}
Now we can add a user to touch the screen to directly access the main interface:

@Overridepublic boolean onTouchEvent(MotionEvent event){if(event.getAction()==MotionEvent.ACTION_UP){Intent intent = new Intent(SplashActivity.this, MainActivity.class);startActivity(intent);finish();}return super.onTouchEvent(event);}

After the test, you will find that if you directly touch the page, the page will be displayed three seconds later.

So we rewrite the code:

package com.example.testsplashdemo;import android.os.Bundle;import android.os.Handler;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.MotionEvent;public class SplashActivity extends Activity{private Handler handler = new Handler();private Runnable runnable;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);handler.postDelayed(runnable = new Runnable(){@Overridepublic void run(){Intent intent = new Intent(SplashActivity.this, MainActivity.class);startActivity(intent);finish();}}, 3000);}@Overridepublic boolean onTouchEvent(MotionEvent event){if(event.getAction()==MotionEvent.ACTION_UP){Intent intent = new Intent(SplashActivity.this, MainActivity.class);startActivity(intent);finish();if (runnable != null)handler.removeCallbacks(runnable);}return super.onTouchEvent(event);}}
A Runnable object is defined, and the callback event is removed after the user clicks.

Handler. postDelay and removeCallback can also be used to determine whether a user can perform the long-pressed operation.

Some friends may ask, I usually send messages to the main interface. Is there any good solution?

Due to the similar principle, I directly paste the Code:

package com.example.testsplashdemo;import android.os.Bundle;import android.os.Handler;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.MotionEvent;public class SplashActivity extends Activity{private Handler handler = new Handler(){public void handleMessage(android.os.Message msg){Intent intent = new Intent(SplashActivity.this, MainActivity.class);startActivity(intent);finish();handler.removeMessages(-1);};};private Runnable runnable;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);handler.sendMessageDelayed(handler.obtainMessage(-1), 3000);}@Overridepublic boolean onTouchEvent(MotionEvent event){if (event.getAction() == MotionEvent.ACTION_UP){handler.sendMessage(handler.obtainMessage(-1));finish();}return super.onTouchEvent(event);}}
It can also be solved.

Here, we will introduce an alternative processing method:

private Handler handler = new Handler(){public void handleMessage(android.os.Message msg){Intent intent = new Intent(SplashActivity.this, MainActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);startActivity(intent);finish();};};

The rest of the code is the same as the above Code, removing the message and adding an Intent flag. When there are only these two activities, it is okay. However, to understand the meaning of FLAG_ACTIVITY_SINGLE_TOP, this Activity is reused when it exists and is located at the top of the stack. That is to say, if the user enters another Activity within three seconds, it will still re-enter.





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.