应用程序级静态变量值不保留在Activity Fragment中分配的新值(Application level static variable value does not retain new va

编程入门 行业动态 更新时间:2024-10-25 04:25:25
应用程序级静态变量值不保留在Activity Fragment中分配的新值(Application level static variable value does not retain new value assigned in Activity Fragment)

'MyApplication'类内部扩展类'Application'

public static int globalflag = 0;

在调试器中,MainActivity启动时'globalflag'的值为0.我转到Fragment'MyFragment'并在其中更改值:

MyApplication.globalflag = 1;

该值也在调试器中更新。 现在,当我按下后退按钮时,调试器中MyApplication.globalflag的值显示为0(不再保留该值)。

如何确保在整个应用程序生命周期中保留此值?

我是Android开发的初学者,非常感谢这个问题的任何输入。 谢谢!

Inside 'MyApplication' Class extending class 'Application'

public static int globalflag = 0;

In the debugger the value of 'globalflag' at the launch of the MainActivity is 0. I go to the Fragment 'MyFragment' and change the value there:

MyApplication.globalflag = 1;

The value updates in the debugger too. Now when I press the back button the value of MyApplication.globalflag in the debugger shows as 0 (no longer retains the value).

How can I make sure this value is retained for the entire Application life cycle?

I am a beginner in Android development and appreciate any input for this problem. Thank you!

最满意答案

首先,我认为这是发生的原因:

您的Application实例生命周期与构成应用程序的组件相关联。

正如明智的沃格拉所说。

应用程序对象是在您的某个Android组件启动时创建的。 它在一个新进程中启动,在唯一用户下具有唯一ID。 即使您未在AndroidManifest.xml文件中指定一个,Android系统也会为您创建一个默认对象。

此外:

应用程序对象在任何组件之前启动,并且至少在应用程序的另一个组件运行时运行。

所以这意味着当你按下Activity上的后退按钮触发Activity的生命周期结束(有效地杀死它)时,Application实例也可能被系统为其资源进行垃圾收集/杀死(因为实际上不需要它) )。

还有其他更好的机制可以在活动/应用程序生命周期(如SharedPreferences或SQLite)之间保留状态

您可以查看本教程以获取有关如何使用SharedPreferences更多详细信息

但很简单:

Context context = getActivity(); SharedPreferences sharedPref = context.getSharedPreferences( getString(R.string.preference_file_key), Context.MODE_PRIVATE);

写入/保存数据:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt(getString(R.string.saved_high_score), newHighScore); editor.commit();

读取持久数据:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); int defaultValue = getResources().getInteger(R.string.saved_high_score_default); long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

First the why I think that is happening:

Your Application instance lifecycle is tied to the components that constitute your app.

As the wise Vogella says.

The application object is created whenever one of your Android components are started. It is started in a new process with a unique ID under a unique user. Even if you do not specify one in your AndroidManifest.xml file, the Android system creates a default object for you.

Moreover:

The application object starts before any component and runs at least as long as another component of the application runs.

So this means that when you press the back button on your Activity triggering the end of life cycle of the Activity (effectively killing it) the Application instance might also be garbage collected/killed by the system for its resources (because effectively it is not needed).

There are other better mechanisms to preserve state between activity / application lifecycle such as SharedPreferences or SQLite

You can check this tutorial for more detail on how to use SharedPreferences

But it's simple:

Context context = getActivity(); SharedPreferences sharedPref = context.getSharedPreferences( getString(R.string.preference_file_key), Context.MODE_PRIVATE);

Writing / saving data:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt(getString(R.string.saved_high_score), newHighScore); editor.commit();

Reading persisted data:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); int defaultValue = getResources().getInteger(R.string.saved_high_score_default); long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

更多推荐

本文发布于:2023-07-27 05:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1285485.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   应用程序   分配   变量值   Activity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!