0

我的应用程序中有两个活动。第一次活动SplashScreen和其他SplashActivity。我有sharedPreferencesSplashScreen但我想在SplashActivity. 我认为如果有可能创建一个SplashActivity只运行一次的方法,即这个方法比较的booleanSplashScreen(就像这在开始时是假的)。在第一次运行它的设置为 true 之后,这个方法被跳过了。我已经尝试了很多来做到这一点,但没有成功。

闪屏-

public class SplashScreen extends Activity 
{
    /** Called when the activity is first created. */
    TimerTask task;
    SharedPreferences pref;
    SharedPreferences.Editor ed;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.splash_screen);
       pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
       Log.v("","onCreate is calling");
       if(pref.getBoolean("activity_executed", false))
       {
            Log.v("","Before if called");
            setContentView(R.layout.splash_screen);
            Log.v("","after if called");
            new Handler().postDelayed(csRunnable1, 5000);
       } 
       else 
       {
          new Handler().postDelayed(csRunnable2, 5000);  
       }
   }

   Runnable csRunnable1=new Runnable() 
   {       
       @Override
       public void run() 
       {
            Intent intent = new Intent(SplashScreen.this, SplashActivity.class);
               startActivity(intent);
               finish();

       }
   };

   Runnable csRunnable2=new Runnable() 
    {      
       @Override
       public void run() 
       {
            Intent intent = new Intent(SplashScreen.this, LoginActivity.class);
               startActivity(intent);
               finish();

       }
   };
}

SplashActivity-

public class SplashActivity extends Activity implements OnClickListener
{   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        //////////////////////////////////////////////////////////////////////
        //////// GAME MENU  /////////////////////////////////////////////////
        Button playBtn = (Button) findViewById(R.id.playBtn);
        playBtn.setOnClickListener(this);
        Button settingsBtn = (Button) findViewById(R.id.settingsBtn);
        settingsBtn.setOnClickListener(this);
        Button rulesBtn = (Button) findViewById(R.id.rulesBtn);
        rulesBtn.setOnClickListener(this);
        Button exitBtn = (Button) findViewById(R.id.exitBtn);
        exitBtn.setOnClickListener(this);
    }


    /**
     * Listener for game menu
     */
    @Override
    public void onClick(View v) {
        Intent i;

        switch (v.getId()){
        case R.id.playBtn :
            //once logged in, load the main page
            //Log.d("LOGIN", "User has started the game");
            //Get Question set //
            List<Question> questions = getQuestionSetFromDb();
            //Initialise Game with retrieved question set ///
            GamePlay c = new GamePlay();
            c.setQuestions(questions);
            c.setNumRounds(getNumQuestions());
            ((CYKApplication)getApplication()).setCurrentGame(c);  
            //Start Game Now.. //
            i = new Intent(this, QuestionActivity.class);
            startActivityForResult(i, Constants.PLAYBUTTON);
            finish();
            break;

        case R.id.rulesBtn :
            i = new Intent(this, RulesActivity.class);
            startActivityForResult(i, Constants.RULESBUTTON);
            finish();
            break;

        case R.id.settingsBtn :
            i = new Intent(this, SettingsActivity.class);
            startActivityForResult(i, Constants.SETTINGSBUTTON);
            finish();
            break;

        case R.id.exitBtn :
            finish();
            break;
        }

    }

我知道这是我需要编辑的行SplashActivity但是每当我添加这个我的应用程序崩溃。

ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.commit();
4

2 回答 2

1

可能是你做错了什么。你可以在启动画面中简单地做到这一点

   if(pref.getBoolean("activity_executed", false))
   {
        Log.v("","Before if called");
        setContentView(R.layout.splash_screen);
        Log.v("","after if called");
        new Handler().postDelayed(csRunnable1, 5000);
        pref.edit().putBoolean("activity_executed", true).commit();
   } 

或者在你的 splashactivity oncreate 中调用这个

getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE).edit().putBoolean("activity_executed", true).commit();
于 2013-10-07T14:48:52.633 回答
0

也在你的 SplashActivity 中调用它

onCreate(...){
....
SharedPreferences pref;
SharedPreferences.Editor ed;
pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.commit();
....
}
于 2013-10-07T14:47:43.493 回答