如何在其他活动中设置SharedPreferences的值?[英] how to set the value of sharedPreferences in other activity?

本文是小编为大家收集整理的关于如何在其他活动中设置SharedPreferences的值?的处理方法,想解了如何在其他活动中设置SharedPreferences的值?的问题怎么解决?如何在其他活动中设置SharedPreferences的值?问题的解决办法?如何在其他活动中设置SharedPreferences的值?问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我的应用程序有两个活动.第一活动SplashScreen和其他SplashActivity.我在SplashScreen中有sharedPreferences,但我想在SplashActivity中设置这个真实的值.我认为如果可以在SplashActivity中创建一个方法,该方法仅运行一次,即该方法比较SplashScreen的boolean值(即在开始时是假的).首次运行它设置为True并跳过此方法.我已经尝试了很多,但不成功.

splashscreen -

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();

推荐答案

可能是你做错了什么.您可以在Splash屏幕中简单地完成此操作

   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();

其他推荐答案

还在splashactivity中调用它

onCreate(...){
....
SharedPreferences pref;
SharedPreferences.Editor ed;
pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.commit();
....
}

本文地址:https://www.itbaoku.cn/post/82126.html

问题描述

I have two activities in my app. First activity SplashScreen and other SplashActivity. I have sharedPreferences in SplashScreen but i want to set value of this true in SplashActivity. I think If it is possible to create a method in SplashActivity which run only once i.e this method compare the boolean value of SplashScreen (like this is false at start). After first run its set to true forever and this method is skipped. I've tried a lot to do this but not successful.

SplashScreen-

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;
        }

    }

I know this is the line i need to edit in SplashActivity but whenever i add this my app crash.

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

推荐答案

May be you are doing something wrong. you can do simply this in splash screen

   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();
   } 

or in your splashactivity oncreate call this

getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE).edit().putBoolean("activity_executed", true).commit();

其他推荐答案

Also call this in your SplashActivity

onCreate(...){
....
SharedPreferences pref;
SharedPreferences.Editor ed;
pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.commit();
....
}
查看更多