我的应用中有两项活动。第一项活动SplashScreen
和其他SplashActivity
。我在sharedPreferences
中有SplashScreen
,但我想在SplashActivity
中设置此值为true。我想如果有可能在SplashActivity
中创建一个只运行一次的方法,那么这个方法比较boolean
的{{1}}值(比如开始时这是假的)。首次将其设置永久运行为true后,将跳过此方法。我已经做了很多努力但没有成功。
闪屏 -
SplashScreen
SplashActivity -
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();
}
};
}
我知道这是我需要在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
答案 0 :(得分: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();
答案 1 :(得分:0)
也请在你的SplashActivity中调用它
onCreate(...){
....
SharedPreferences pref;
SharedPreferences.Editor ed;
pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.commit();
....
}