启动活动只有一次 [英] Start activity only once

查看:135
本文介绍了启动活动只有一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的应用程序启动一个活动仅在第一次应用程序启动。 任何一个有任何想法?

I want my app to start a activity only the first time the app launches. Any one got any idea?

我发现这一点,但只显示黑屏。

i found this but it only shows a black screen.

public class WhatsNew extends Activity {

public static final String PREFS_NAME = "MyPrefsFile";

protected void onCreate(Bundle state){

       super.onCreate(state);
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean dialogShown = settings.getBoolean("dialogShown", false);

       if (!dialogShown) {
         // AlertDialog code here

         SharedPreferences.Editor editor = settings.edit();
         editor.putBoolean("dialogShown", true);
         editor.commit();    
       }
    }

}

推荐答案

我想请您谈一下,如日志页面上的活动,您将启动只有一次在应用程序的整个生活。 对于这一点,你可以使用共享preferences:

I think that you talk about activities like "log" page that you would launch only once in your application's whole life. For this, you can use sharedPreferences:

SharedPreferences prefs;
SharedPreferences.Editor editor;

和下方的startActivity(意向),添加:

and below your startActivity(intent), you add:

prefs = getSharedPreferences("nbRepet",Context.MODE_PRIVATE);
editor = prefs.edit();
editor.putInt("nbRepet", 1);
editor.commit();

现在你的变量nbRepet有1的值。

Now your variable nbRepet have 1 as value.

在这之后,你可以在上面你startActivity(意向)添加这些行,以验证您的活动一直没有推出之前:

After that, you can add these lines ABOVE your startActivity(intent) to verify that your activity was never launched before:

//在这里恢复nbRepet的价值。

//here you recover nbRepet's value..

preferences = MainActivity.this.getSharedPreferences("nbRepet", MODE_PRIVATE);      
int value = preferences.getInt("nbRepet", 0);

// ..你验证,如果你的活动之前推出。

//.. and you verify if your activity is launched before.

if(value<1)
{
    startActivity(intent);
}

这篇关于启动活动只有一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆