我希望我的应用程序有一个 Activity ,该 Activity 显示有关如何使用该应用程序的说明。但是,此“说明”屏幕在安装后只能显示一次,您该怎么做?

最佳答案

您可以测试应用程序firstRun中是否设置了特殊标志(我们将其称为SharedPreferences)。如果不是,这是第一次运行,因此请按照说明显示您的 Activity /弹出窗口/任何内容,然后在首选项中设置firstRun

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences settings = getSharedPreferences("prefs", 0);
    boolean firstRun = settings.getBoolean("firstRun", true);
    if ( firstRun )
    {
        // here run your first-time instructions, for example :
        startActivityForResult(
             new Intent(context, InstructionsActivity.class),
             INSTRUCTIONS_CODE);

    }
 }



// when your InstructionsActivity ends, do not forget to set the firstRun boolean
 protected void onActivityResult(int requestCode, int resultCode,
         Intent data) {
     if (requestCode == INSTRUCTIONS_CODE) {
         SharedPreferences settings = getSharedPreferences("prefs", 0);
         SharedPreferences.Editor editor = settings.edit();
         editor.putBoolean("firstRun", false);
         editor.commit();
     }
 }

09-26 04:19