How to introduce and use the running framework when the Android app is started
How to introduce and use the running framework when the Android app is started
Introduction
The running framework of the Android app refers to some code or operations that automatically executes when the APP starts.It can be used to perform tasks such as initialization operations, checking user login status, and data loading to provide users with a better use experience.
There are two types of running frameworks when the common Android App starts: the Application class and the startup page (Splash Screen).The Application class is a singles object created by the Android system when the application starts. It can be initialized by inheriting the Application class and rewriting its oncreate () method.The startup page is an independent Activity. When the application starts, it will display the startup page, then perform some initialization operations, and finally jump to the main interface.
How to use
The methods of using the Application class and startup page are introduced below:
1. Application class
First, register the custom Application class in the AndroidManifest.xml file.Add "Android: name" attribute to the <Application> tag, and the value is a full name of a custom Application class.
<application
...
android:name=".MyApplication"
...>
Next, create a custom class that inherits the Application class and rewrite its oncreate () method.Some initialization operations can be performed in the oncreate () method.
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Do some initialization operations here
// For example, initialize the network request library, database, etc.
}
}
Finally, you can obtain the Application object in any other components (such as Activity, Service, etc.), and call the method provided or access its member variables.
MyApplication myApp = (MyApplication) getApplication();
myApp.doSomething();
2. Start page
First of all, the startup page is set to the application of the application in the AndroidManifest.xml file, that is, it is set to Launcher Activity.
<activity
android:name=".SplashActivity"
...>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Next, create an Activity of the startup page and perform some initialization operations in it.
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// Perform some initialization operations here
// For example, check the user login status, perform data loading, etc.
// Time jump to the main interface
new Handler().postDelayed(() -> {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}, 2000); // Delay 2 seconds and jump after 2 seconds
}
}
In the above example, we use Handler to delay for 2 seconds and jump to the main interface. You can set the delay time according to the actual needs.
3. Summary
By using the Application class and startup page, we can perform some initialization operations when the Android App is started to enhance the user experience.It is relatively simple to use the Application class, suitable for global initialization operations; and using the startup page can display some welcome information or load animations on the interface to provide users with better visual effects.