• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Application Level Variables

I need to be able to share a collection of a custom class between activities. I've seen many suggest extending the Application class and storing the variables in there. Unfortunately my application is blowing up on start up when I attempt this. My question is twofold, 1) Is there another way to do this, 2) Am I doing something wrong? I am using monodroid to develop this. This is the class extending the Application class. It does nothing right now because I trimmed it down to try and eliminate possible causes.

Code:
    public class MyApplication : Application
    {
       
    }

Here is my Manifest.xml
Code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="internalOnly" package="foo.bar" android:versionCode="1" android:versionName="1.0">
  <application android:name=".MyApplication" android:label="MonoDroidTestApp">
  </application>
  <uses-sdk android:minSdkVersion="8" />
</manifest>

That's all it takes to blow things up. Supposedly this should create an instance of my extended class on application start up that I can access from any activity.
 
What error do you get when it "blows up"...

If it actually blows up, you're screwed...
heh :D

I forget what the error was, but it was terminating on start up. Someone pointed me in another direction that worked great though.

I now have my main activity with private static members and get/set functions to access them. The other activities simply extend the main activity class and use the properties. I'm new to this, so it was an exciting thing to figure out.
 
Actually you can seperate the app-wide classes into just classes that dont extend anything and set the functions and members to public static (and final for constants).

This is probably better that storing static getters and setters in activities as Dalvik really likes to reclaim memory from Activities. Activities are very large memory hogs and thus a small object or static class may be btter for holding a variable you want accross your app.

Getters and setters are also a minor performance hit as compared to direct member access (at least internally).

Designing for Performance | Android Developers


Either way, just be sure to check for null (always a good idea with everything almost in java) when accessing them.


This is pretty much how my entire config and util packages work.
 
Actually you can seperate the app-wide classes into just classes that dont extend anything and set the functions and members to public static (and final for constants).

This is probably better that storing static getters and setters in activities as Dalvik really likes to reclaim memory from Activities. Activities are very large memory hogs and thus a small object or static class may be btter for holding a variable you want accross your app.

Getters and setters are also a minor performance hit as compared to direct member access (at least internally).

Designing for Performance | Android Developers


Either way, just be sure to check for null (always a good idea with everything almost in java) when accessing them.


This is pretty much how my entire config and util packages work.
Thanks, I will definitely redesign this way. It makes perfect sense. I didn't really like the idea of having that mixed in with my activity code anyway.
 
Back
Top Bottom