简体   繁体   English

从另一个活动更新 SharedPreferences

[英]Updating a SharedPreferences from another activity

I have two activities.我有两个活动。 In the first activity, I have a SharedPreferences with a TextView, which displays the amount of data in the SharedPreferences.在第一个活动中,我有一个带有 TextView 的 SharedPreferences,它显示 SharedPreferences 中的数据量。 These codes are written in the onCreate()这些代码写在 onCreate()

SharedPreferences coin = getSharedPreferences("saved", MODE_PRIVATE);
SharedPreferences.Editors editor2 = coin.edit();
editor2.putInt("t", 0).apply();
editor.commit();

TextView name = (TextView) findViewById(R.id.textview);

Integer a = coin.getInt("t", 0);
name.setText(a.toString());

In the second activity, I wanna add 10 points to the data and update the SharedPreferences.在第二个活动中,我想向数据添加 10 个点并更新 SharedPreferences。 These codes are not inside onCreate().这些代码不在 onCreate() 中。

SharedPreferences coin = getSharedPreferences("saved", MODE_PRIVATE);
SharedPreferences.Editors editor2 = coin.edit();

int a = coin.getInt("t", 0);
int b = a + 10;
editor2.putInt("t", Integer.valueOf(b));

The problem is, after adding 10 units there is no update in the TextView so I don't even know if the 10 units have been added correctly to SharedPreferences or not.问题是,添加 10 个单位后,TextView 中没有更新,所以我什至不知道这 10 个单位是否已正确添加到 SharedPreferences。

Please help请帮忙

If I understand, you would like reflect the final value of t in SharedPreference in your first activity after its updated in second activity?如果我理解,您想在第二个活动中更新后,在您的第一个活动中反映SharedPreferencet的最终值?

To do that, create a function updateNameTV() which would contain logic to update the TextView with the value of t from SharedPreference and call that function in onResume() of the FIRST activity like:为此,请创建一个函数updateNameTV() ,该函数将包含使用SharedPreferencet值更新 TextView 的逻辑,并在 FIRST 活动的onResume()中调用该函数,例如:

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub

        updateNameTV();

        super.onResume();
    }

So when Activity 2 is launched, Activity 1 goes in back of the stack and onPause() gets called.. when we return to Activity 1 this activity is pop back and onStart() followed by onResume() is called.因此,当 Activity 2 启动时,Activity 1 进入堆栈的后面,并且onPause()被调用......当我们返回 Activity 1 时,这个 Activity 被弹回并且onStart()onResume()被调用。 These are the lifecycle events more here这些是生命周期事件更多here

So the plan is, when onResume() is called we update the TextView value with the latest SharedPreference And we do that before calling super.onResume() such that update is performed before handing over the control to next lifecycle event.所以计划是,当onResume()被调用时,我们用最新的SharedPreference更新 TextView 值,我们在调用super.onResume()之前这样做,以便在将控制权移交给下一个生命周期事件之前执行更新。 Hope this gives more clarity.希望这能让你更清楚。

You have to register OnSharedPreferenceChangeListener in your first activity in order to listen to the changes and then update your TextView with the updated value.您必须在第一个活动中注册OnSharedPreferenceChangeListener以监听更改,然后使用更新后的值更新您的 TextView。

Implement OnSharedPreferenceChangeListener in your FirstActivity:实施OnSharedPreferenceChangeListener在FirstActivity:

public class FirstActivity extends AppCompatActivity implements
        SharedPreferences.OnSharedPreferenceChangeListener {
    ...

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
        // This method will be called when there is a change in SharedPreference
        // Update your textview here
        if(s.equals("t")) {
            Integer a = sharedPreferences.getInt("t", 0);
            name.setText(a.toString());
        }
    }
}

In your FirstActivity's onStart() and onStop() method, manage the listener:在 FirstActivity 的onStart()onStop()方法中,管理监听器:

    @Override
    protected void onStart() {
        super.onStart();
        getSharedPreferences("saved", MODE_PRIVATE).registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onStop() {
        getSharedPreferences("saved", MODE_PRIVATE).unregisterOnSharedPreferenceChangeListener(this);
        super.onStop();
    }

But remember, once you leave your FirstActivity, the listener gets unregistered and you will no longer receive updates.但请记住,一旦您离开 FirstActivity,侦听器将取消注册,您将不再收到更新。 To reflect the updates even after the activity go background, you can just update your textview in your onStart() so that when the FirstActivity starts, the textview gets updated with the latest value from SharedPreferences:为了在活动进入后台后反映更新,您可以在onStart()更新您的 textview,以便在 FirstActivity 启动时,使用 SharedPreferences 中的最新值更新 textview:

    @Override
    protected void onStart() {
        super.onStart();
        SharedPreferences coin = getSharedPreferences("saved", MODE_PRIVATE);
        coin.registerOnSharedPreferenceChangeListener(this);
        // Also Update the textview here
        Integer a = coin.getInt("t", 0);
        name.setText(a.toString());
    }

Now, any update to the preference from anywhere will also change the textview text as soon as the FirstActivity comes into foreground or launches.现在,只要 FirstActivity 进入前台或启动,任何地方对首选项的任何更新也将更改 textview 文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM