如何更改另一个活动的变量?

发布于 2024-12-03 00:13:14 字数 195 浏览 1 评论 0原文

所以我有两个活动:主要是所有数据,次要是首选项。我希望从“首选项”活动中访问主活动中的两个数组并使用这两个数组。事实上,我需要将数组中的所有数据重置为默认值。有什么办法可以做到吗?

我尝试使用 SharedPreferences 因为我将所有这些数据存储在其中。我从“首选项”活动中更改了其中的值,但我猜主活动中的数据没有更改,因为它没有重新启动,只是暂停了。

So I have two activities: main with all data and secondary with Preferences. There are two arrays in main activity that I want to have access to from Preferences activity and work with this two arrays. In fact, I need to reset all data in arrays to default. Is there any way to do it?

I've tried to use SharedPreferences because I store all this data in it. I changed values in it from Preferences activity, but data in main activity haven't changed, I guess, because it's not being restarted, just paused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逐鹿 2024-12-10 00:13:14

您可以使用 SharedPreferences 和 注册OnSharedPreferenceChangeListener 和/或更新 Activity 的 onResume() 方法中的数据。

这是一些基本示例:

public class MainActivity extends Activity implements OnSharedPreferenceChangeListener {
    private SharedPreferences prefs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // get your SharedPreferences objects
        prefs = ...;
    }

    // This method will be called every time your activty comes to the front
    @Override
    protected void onResume() {
        super.onResume();

        // register a listener to get notified when the preferences change
        prefs.registerOnSharedPreferenceChangeListener(this);

        // do whatever you need to do to update the data
        updateData();
    }

    @Override
    protected void onPause() {
        super.onPause();

        // unregister listener if another activity comes to the front to save battery
        prefs.unregisterOnSharedPreferenceChangeListener(this);
    }

    // this method will be called when some preference changes
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        // do whatever you need to do to update the data
        updateData();
    }
}

You can use SharedPreferences and register a OnSharedPreferenceChangeListener and/or update the data in the onResume() method of your Activity.

Here's some basic example:

public class MainActivity extends Activity implements OnSharedPreferenceChangeListener {
    private SharedPreferences prefs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // get your SharedPreferences objects
        prefs = ...;
    }

    // This method will be called every time your activty comes to the front
    @Override
    protected void onResume() {
        super.onResume();

        // register a listener to get notified when the preferences change
        prefs.registerOnSharedPreferenceChangeListener(this);

        // do whatever you need to do to update the data
        updateData();
    }

    @Override
    protected void onPause() {
        super.onPause();

        // unregister listener if another activity comes to the front to save battery
        prefs.unregisterOnSharedPreferenceChangeListener(this);
    }

    // this method will be called when some preference changes
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        // do whatever you need to do to update the data
        updateData();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文