Hi Narcís,
In my code I have: [Application] public class MyApplication : Application { ... public static readonly MyApplication theApp = new MyApplication (); ... public MyApplication (IntPtr intPtr) :base(intPtr) { } ... } In various places its properties get referenced like: MyApplication.theApp.layout If you put tracing inside the constructor for MyApplication youll see the object being constructed when the app starts, but then also again if Android closed the app for its own reasons (or if there was a crash) and later on re-starts it without the user initiating things. In your case that might work better than creating an instance of MyApplication which isnt a singleton. But you also need to bear in mind that MyApplication might get constructed at strange times: With the Activity lifecycle the activities exist independently of each other and are OnCreated by Android without you being able to have a stack like you do on Windows with ShowDialog. Hence if your current activity is OnDestroyd by the OS because of, say, memory issues then the next youll know about it will be an OnCreate and, quite possibly, your MyApplication constructor will get called again at that point, And youll need to re-initialise things (eg. data access layers or background threads) that you may be holding at this global level. (In the same way, you can have a stack of activities which look as though they all exist at the same time and the stack unwinds as activities are Finished, but really the only activity you can rely on is your current one, any others may well get destroyed and will get re-created just in time by the OS to process OnActivityResult etc. So you cant rely on any references to those other activities.) Anyway, thats my understanding at least. These are all things that were a culture shock to me as a Windows programmer, maybe theyre obvious to everybody else! Cheers, Andy From: monodroid-boun...@lists.ximian.com [mailto:monodroid-boun...@lists.ximian.com] On Behalf Of Narcís Calvet Sent: 27 July 2011 14:49 To: 'Discussions related to Mono for Android' Subject: Re: [mono-android] Getting objects from an intent Hi Andy, Following your advice Im using the custom application below. It has a public property (layout) which I use to add to the main Activity. The problem is this property is always null there. Application is bein used in main Activity OnCreate event like this: var ctrl = new MyApplication(); SetContentView(ctrl.layout); MyApplication implementation: [Application] public class MyApplication : Application { public RelativeLayout layout { get ; set; } public MyApplication(IntPtr handle) : base(handle) { } public MyApplication() : base() { } public override void OnCreate() { base.OnCreate(); MyImageView i = new MyImageView(this.ApplicationContext); MyImageView i2 = new MyImageView(this.ApplicationContext); layout = new RelativeLayout(this); layout.AddView(i, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FillParent, RelativeLayout.LayoutParams.FillParent)); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(400, 400); lp.LeftMargin = 100; lp.TopMargin = 100; layout.AddView(i2, lp); } } Do you have any idea how should I use this property in the parent Activity? I tried doing the same in Java for Android and cant even get the onCreate event firing there. Best Regards, Narcís Calvet Steema Software http://www.steema.com <http://www.steema.com/> http://twitter.com/SteemaSoftware https://www.facebook.com/SteemaSoftware From: monodroid-boun...@lists.ximian.com [mailto:monodroid-boun...@lists.ximian.com] On Behalf Of Andrew Sinclair Sent: dimarts, 19 / juliol / 2011 14:01 To: 'Discussions related to Mono for Android' Subject: Re: [mono-android] Getting objects from an intent Narcís, You can derive from Android.App.Application, this will give you a singleton class which can hold global application state. For such a class you need to use the [Application] attribute - this will add the appropriate entry in the AndroidManifest.xml. Storing the instance of an activity doesnt seem much use in Android because activities are created and destroyed regularly by the system in accordance with the activity lifecycle. So if you want to access a property of one activity from another activity then theres a very good chance that the target activity will be invalid. This might be different with activity groups, I havent used them, but in general Ive found it best to store data in an instance-independent manner. The same things applies with Views. Suppose you had 2 views within an activity that both cause another activity to be shown, and you wanted to remember which view had triggered this new activity. Within Windows you might have a member variable of the calling activity called _triggeringView and youd access this when you returned to the original activity. With Android thats no use because you have to assume that the original activity will be destroyed during the whole process. One way around this is to store something like triggering view was the 2nd view at the application level or as part of the saved state bundle in the activity. So youre just storing an integer. Clear as mud? I hope it helps anyway, the implications of the activity life cycle are very far reaching if you are porting an application from, for example, .NETCF. Andy From: monodroid-boun...@lists.ximian.com [mailto:monodroid-boun...@lists.ximian.com] On Behalf Of Narcís Calvet Sent: 19 July 2011 12:28 To: 'Discussions related to Mono for Android' Subject: Re: [mono-android] Getting objects from an intent Searching a little bit and looking for whats available in IParcelable it seems it is not complete in Mono for Android so I guess I should discard this option. Any other ideas? Thanks! Best Regards, Narcís Calvet Steema Software http://www.steema.com <http://www.steema.com/> http://twitter.com/SteemaSoftware https://www.facebook.com/SteemaSoftware From: monodroid-boun...@lists.ximian.com [mailto:monodroid-boun...@lists.ximian.com] On Behalf Of Narcís Calvet Sent: dimarts, 19 / juliol / 2011 11:04 To: 'Discussions related to Mono for Android' Subject: [mono-android] Getting objects from an intent Hello, I have created a custom activity which I use in an ActivityGroup using Intents. How can I get the instance of my custom activity the intent has created to be able to use its properties and methods? I read I may need to use Serializable or Parcelable interfaces but havent succeed on this so far. Monodroid examples on this would be great! Thanks in advance. Best Regards, Narcís Calvet Steema Software http://www.steema.com <http://www.steema.com/> http://twitter.com/SteemaSoftware https://www.facebook.com/SteemaSoftware
_______________________________________________ Monodroid mailing list Monodroid@lists.ximian.com UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid