在两个活动之间保存数据

时间:2014-07-05 19:38:03

标签: android

之前请注意:Trying to pass data between activities 好的,所以我在第一个活动上有这个代码:

           prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
           coins = prefs.getInt("key2", 0);

和此:

    Intent i = new Intent(getApplicationContext(), Shop.class);
    i.putExtra("new_variable_name",coins);
    startActivity(i);

在第二项活动中:

     private int coinsValue = 0;
     ....
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.XXXX);
     Bundle extras = getIntent().getExtras();
     if (extras != null) {
     coinsValue = getIntent().getIntExtra("new_variable_name", 0);
     .... }

此代码工作正常,硬币在两个页面上共享,现在我想回到第一个活动时,硬币和coinsValue会同步,它会保存它,我的意思是,当我去的时候这个代码可以工作从第一个活动到第二个活动,但是当我回到第一个活动(从商店到第一个活动的意图)时,硬币值保持与开始时相同,它不保存coinsValue。

编辑:

第一个活动代码:

public SharedPreferences prefs;
public static final int REQUEST_CODE = 1;
....
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.PAUSE:
            openPopUP(); 
            return true;
        case R.id.SHOP:
            openSHOP();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
    .....
    private void openSHOP() {
    mPlayer.pause();
    Context context = getApplicationContext();
    Intent i = new Intent(context, Shop.class);
    i.putExtra("new_variable_name",coins);
    startActivityForResult(i, REQUEST_CODE);
    CharSequence text = "You are at the shop";
    int duration = Toast.LENGTH_LONG;
    Toast.makeText(context, text, duration).show();
      }
    .......
    prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    score = prefs.getInt("key", 0); //0 is the default value
    coins = prefs.getInt("key2", 0);
    time = prefs.getInt("key1", 0);
    .............. }

第二个活动代码:

           public static final int REQUEST_CODE = 1;
           ......
           Bundle extras = getIntent().getExtras();
           if (extras != null) {
           coinsValue = getIntent().getIntExtra("new_variable_name", 0);
           ..........
            if (id == R.id.button6) {   
     mPlayer.pause();
     startActivityForResult(getIntent(), REQUEST_CODE);
     Intent intent = new Intent(Shop.this, FirstActivity.class);
     intent.putExtra("key2", coinsValue);
     setResult(RESULT_OK, intent);      
     startActivity(intent);
     Context context = getApplicationContext();
     CharSequence text = "You went back to the game";
     int duration = Toast.LENGTH_LONG;
     Toast toast = Toast.makeText(context, text, duration);
     toast.show();     
     }

与此同时,仍然没有工作,当我进入第二个活动时,它确实知道我有多少硬币,但是当我花掉它们并回到第一个活动时,它显示出我的避风港#39; t什么都不买,就像它在购买后没有保存硬币一样。 :)

3 个答案:

答案 0 :(得分:0)

为此,我建议您使用SharedPreferences,就像之前的问题一样。无论如何,您可以使用startAcvitityForResult(intent, yourRequestCode);

将数据返回到之前的Acvitity:

Intent intent = new Intent();
intent.putExtra("your_key", key_value);
setResult(RESULT_OK, intent);
finish();

以前的Acvitity:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK)
        {
            if(requestCode == yourRequestCode && data != null)
            {
             coins = data.getIntExtra("your_key",default_value);   
            }
        }
    }

编辑:更改了代码。 第一项活动:

public static final int REQUEST_CODE = 1;
      ...........
    private void openSHOP() {
    mPlayer.pause();

    Context context = getApplicationContext();

    Intent i = new Intent(context, Shop.class);
    i.putExtra("new_variable_name",coins);
    startActivityForResult(i, RESULT_CODE);

    CharSequence text = "You are at the shop";
    int duration = Toast.LENGTH_LONG;
    Toast.makeText(context, text, duration).show();

      }
     ....
    prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    score = prefs.getInt("key", 0);
     ....
        @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK)
    {
        if(requestCode == REQUEST_CODE && data != null)
        {
         coins = data.getIntExtra("key2", coins);   
        }
    }
   }

第二项活动:

{
    Bundle extras = getIntent().getExtras();
     if (extras != null) {
     coinsValue = getIntent().getIntExtra("new_variable_name", 0);
     ....
     setResultAndFinish();
}

private void setResultAndFinish()
    {
        Intent intent = new Intent();
        // use your number of coins and put it to Intent.
        intent.putExtra("key2", coinsValue);
        setResult(RESULT_OK, intent);
        finish();
    }

答案 1 :(得分:0)

如果您在SharedPreferences中保存您的硬币值,您只需在更改时保存它,并在需要时从“首选项”中读取它。

要保存,请使用:

prefs.edit().putInt("key2", coinsValue).commit();

阅读,就像你在第一个Activity中所做的那样:

coins = prefs.getInt("key2", 0);

通过这种方式,您可以随时随地获取更新的硬币值。

答案 2 :(得分:0)

请改用SharePreferences。它也可以在其他活动中访问。没有必要将它发送到您在SharedPreferences

中存储的位置

可以在整个应用程序中访问共享首选项,因此您可以从应用程序中的任何活动中读取它们。

在活动A中存储键/值对:

SharedPreferences settings = getSharedPreferences("mysettings", 
     Context.MODE_PRIVATE);

SharedPreferences.Editor editor = settings.edit();
editor.putString("mystring", "wahay");
editor.commit();

从另一项活动中读取此值:

SharedPreferences settings = getSharedPreferences("mysettings", 
    Context.MODE_PRIVATE);
String myString = settings.getString("mystring", "defaultvalue");

您可以在http://developer.android.com/guide/topics/data/data-storage.html#pref

找到更多信息