我的应用程序中有两个活动。第一次活动SplashScreen
和其他SplashActivity
。我有sharedPreferences
,SplashScreen
但我想在SplashActivity
. 我认为如果有可能创建一个SplashActivity
只运行一次的方法,即这个方法比较的boolean
值SplashScreen
(就像这在开始时是假的)。在第一次运行它的设置为 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();