How to declare global variables in Android?

前端 未结 17 2934
死守一世寂寞
死守一世寂寞 2020-11-21 04:12

I am creating an application which requires login. I created the main and the login activity.

In the main activity onCreate method I added the following

相关标签:
17条回答
  • 2020-11-21 04:54

    What about ensuring the collection of native memory with such global structures?

    Activities have an onPause/onDestroy() method that's called upon destruction, but the Application class has no equivalents. What mechanism are recommended to ensure that global structures (especially those containing references to native memory) are garbage collected appropriately when the application is either killed or the task stack is put in the background?

    0 讨论(0)
  • 2020-11-21 05:00

    you can use Intents , Sqlite , or Shared Preferences . When it comes to the media storage, like documents , photos , and videos, you may create the new files instead.

    0 讨论(0)
  • 2020-11-21 05:02

    Just a note ..

    add:

    android:name=".Globals"
    

    or whatever you named your subclass to the existing <application> tag. I kept trying to add another <application> tag to the manifest and would get an exception.

    0 讨论(0)
  • 2020-11-21 05:02

    I couldn't find how to specify the application tag either, but after a lot of Googling, it became obvious from the manifest file docs: use android:name, in addition to the default icon and label in the application stanza.

    android:name The fully qualified name of an Application subclass implemented for the application. When the application process is started, this class is instantiated before any of the application's components.

    The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base Application class.

    0 讨论(0)
  • 2020-11-21 05:03

    DO N'T Use another <application> tag in manifest file.Just do one change in existing <application> tag , add this line android:name=".ApplicationName" where, ApplicationName will be name of your subclass(use to store global) that, you is about to create.

    so, finally your ONE AND ONLY <application> tag in manifest file should look like this :-

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.NoActionBar"
            android:name=".ApplicationName"
            >
    
    0 讨论(0)
  • 2020-11-21 05:06

    Like there was discussed above OS could kill the APPLICATION without any notification (there is no onDestroy event) so there is no way to save these global variables.

    SharedPreferences could be a solution EXCEPT you have COMPLEX STRUCTURED variables (in my case I had integer array to store the IDs that the user has already handled). The problem with the SharedPreferences is that it is hard to store and retrieve these structures each time the values needed.

    In my case I had a background SERVICE so I could move this variables to there and because the service has onDestroy event, I could save those values easily.

    0 讨论(0)
提交回复
热议问题