检测首次运行[英] Detect First Run

本文是小编为大家收集整理的关于检测首次运行的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试检测我的应用程序是否已经运行,通过使用此代码:

(这是我的默认Android活动)

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    if (savedInstanceState == null) {
        Log.w("activity", "first time");
        setContentView(R.layout.activity_clean_weather);
    } else {

        Log.w("activity", "second time");
        setContentView(R.layout.activity_clean_weather);
    }


 }

当我第一次运行应用程序时,它第一次说,当我第二次运行它时,第一次和第三次运行它第一次....

我正在使用实际的Android设备,我每次都没有使用运行按钮.我用Eclipse RUN按钮运行APP一次,然后我关闭应用程序,然后按下手机上的图标.

我的代码有问题吗?

推荐答案

savedInstanceState更多用于在状态之间切换,如暂停/恢复,那种东西.它必须始终由您创建.

在这种情况下你想要的是 SharedPreferences .

这样的东西:

public static final String PREFS_NAME = "MyPrefsFile"; // Name of prefs file; don't change this after it's saved something

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); // Get preferences file (0 = no option flags set)
    boolean firstRun = settings.getBoolean("firstRun", true); // Is it first run? If not specified, use "true"

    if (firstRun) {
        Log.w("activity", "first time");
        setContentView(R.layout.activity_clean_weather);

        SharedPreferences.Editor editor = settings.edit(); // Open the editor for our settings
        editor.putBoolean("firstRun", false); // It is no longer the first run
        editor.commit(); // Save all changed settings
    } else {
        Log.w("activity", "second time");
        setContentView(R.layout.activity_clean_weather);
    }

}

我基本上直接从存储选项的文档并将其应用于您的情况.早期学习是一个很好的概念.

其他推荐答案

您可以使用自定义的共享首选项来存档您的目标.

其他推荐答案

事实是savedInstanceState跨活动保持持久数据.因此,如果您重新启动应用程序,则savedInstanceState将跨运行为空.您应该使用Preference或某些数据库条目来跟踪您的第一次运行.我自己为此目的使用SharedPreference.

本文地址:https://www.itbaoku.cn/post/102615.html

问题描述

I am trying to detect if my app has been run before, by using this code:

(This is in my default Android activity)

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    if (savedInstanceState == null) {
        Log.w("activity", "first time");
        setContentView(R.layout.activity_clean_weather);
    } else {

        Log.w("activity", "second time");
        setContentView(R.layout.activity_clean_weather);
    }


 }

When I first run the app it says first time, when I run it a second time, first time, and a third, first time....

I am using an actual Android device and I am not using the run button each time. I run the app once with the Eclipse run button, then I close the app and press on its icon on my phone.

Is there something wrong with my code?

推荐答案

savedInstanceState is more for switching between states, like pausing/resuming, that kind of thing. It must always be created by you, also.

What you want in this case is SharedPreferences.

Something like this:

public static final String PREFS_NAME = "MyPrefsFile"; // Name of prefs file; don't change this after it's saved something

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); // Get preferences file (0 = no option flags set)
    boolean firstRun = settings.getBoolean("firstRun", true); // Is it first run? If not specified, use "true"

    if (firstRun) {
        Log.w("activity", "first time");
        setContentView(R.layout.activity_clean_weather);

        SharedPreferences.Editor editor = settings.edit(); // Open the editor for our settings
        editor.putBoolean("firstRun", false); // It is no longer the first run
        editor.commit(); // Save all changed settings
    } else {
        Log.w("activity", "second time");
        setContentView(R.layout.activity_clean_weather);
    }

}

I basically took this code directly from the documentation for Storage Options and applied it to your situation. It's a good concept to learn early.

其他推荐答案

You may use a self-defined shared preference to archive your goal.

其他推荐答案

The fact is that savedInstanceState holds persistent data across activities. As such if you restart the app, savedInstanceState will be null across runs. You should either use a Preference or some data base entry to keep track of your first run. I myself use a SharedPreference for this purpose.