Android Development Notes: Splash realizes the quick opening function of the homepage

Keywords: Android Java less Google

When we are developing Android, there are two ways to open the homepage. One is to use the handler to delay an activity. When the time arrives, the software will jump to the second activity. The other is a more commonly used method, which uses splash to realize the quick opening of the homepage. This way of opening the screen is faster than the way of handler to pass in pictures, and the time for white screen is less. It is also the way recommended by Google in 2018 developer conference. So let's see how this works.

I. MainActivity.java

This program is the first one we need to open the screen

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//hide the status bar
        getSupportActionBar().hide();//Hide title block
        setContentView(R.layout.activity_main);
        Thread myThread=new Thread(){//Create child thread
            @Override
            public void run() {
                try{
                    sleep(5000);//Sleep the program for five seconds
                    Intent it=new Intent(getApplicationContext(),PrimaryColor.class);//start-up MainActivity,Then jump the activity to PrimaryColor.java
                    startActivity(it);
                    finish();//Close current activity
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        myThread.start();//Startup thread
    }


    }

2. At the same time, check the Android menifest.java file as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lenovo.deeplove">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".PrimaryColor"></activity>
    </application>

</manifest>

In this way, our splash screen can be opened quickly. Its operation effect is as follows. It is much faster than using the handler. We can see clearly that as soon as the mouse clicks on the software, the pictures will pop up without any white screen loading

Posted by Simsonite on Sun, 24 Nov 2019 13:47:36 -0800