Android: how to determine cold start

2019-07-24 19:37发布

When app being launched, how to determine it is cold start or not? By cold start, I mean the app launched from the very beginning, instead of just resumed from a previous state.

:Edit: elaborate my question: I'm trying to find the app's launch mode: cold start or warm start. Cold start means the app is never launched before or not in background, so it needs a complete launch. Warm start means it is still in background, so it can resume and start faster. How to distinguish these two start modes programmatically?

2条回答
我只想做你的唯一
2楼-- · 2019-07-24 19:51

I learnt about Cold Start in Android while working on Android Development at Oodles Technologies. Let me share my knowledge.

Splash Screen Also Known as Cold Start

Splash screen is an activity that will show for some time when your app is starting and after some time redirect to application main activity.

When to use splash screen :

To show your brand logo user. To load data from network or other source while showing splash screen. Android splash screen is used so that apps can show their icons before showing other content of the app. Splash is also used to do some background work in application for example loading resources from network while the splash screen is being shown. It will give a smooth look to the app.

Cold Start is also known as splash screen,launch screen or cold start in android.As per my opinion, users should have the content available as soon as possible for any app, but when a user launch an application, Android creates a new process , during it shows a black or white screen to user which is built in with application theme, or the theme of the activity that is the entry point.There can be more load if our application is more complex and using application object,which is normally used to initialize the analytics, error reporters, etc. i.e why black or white screen is not a good thing to show to user. If application load time is slow, we could use a place holder to simply fill it by real content, or on the other hand, if our workload is complex, we could show the logo of our application to make application look beautiful.

As we have discussed before, the window displayed by the window manager when the process is in the loading state is set up in the application theme(values>Style section).

Specifically with the value inside android:WindowBackground, you can make the splash or cold start by making a with the color of the background of the main activity over a small bitmap in the center.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
    <item android:drawable="@color/colorAccent" />
    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_notification" />
    </item>
</layer-list>

The layer_list must be opaque .And the background color should be filled with a color in your main layout, if not the layerlist will always shown in the activity.

activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorAccent"
    >

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?colorPrimary"
        android:elevation="4dp"/>
</LinearLayout>

SplashActivity.java

package com.weone.android.controllers.activities;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;

import com.crashlytics.android.Crashlytics;
import com.crashlytics.android.core.CrashlyticsCore;
import com.weone.android.BuildConfig;
import com.weone.android.R;
import com.weone.android.utilities.database.MyPrefs;
import com.weone.android.utilities.network.gcm.GcmHandlerActivity;

import butterknife.ButterKnife;
import io.fabric.sdk.android.Fabric;

/**
 * Created by oodles on 15/1/16.
 */
public class SplashActivity extends GcmHandlerActivity {
    Intent intent;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.top_activity);     
        ButterKnife.bind(this);
        initViews();
    }

      private void initViews() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {              
                    intent = new Intent(mContext, DrawerActivity.class);
                    startActivity(intent);
                    finish();              
            }
        }, 1000);
    }
}
查看更多
祖国的老花朵
3楼-- · 2019-07-24 20:08

If the app is still running in the background it will call onResume first and not onCreate at all.

If you mean the first time it was ever launched on a device then use sqlite or shared preferences to store data and at the beginning check to see if its there.

查看更多
登录 后发表回答