Hi Narcís,

 

So Layout is always null? I’d try:

 

    public RelativeLayout Layout

    {

      get

      {

                if (layout == null)

{

                                CreateLayout();

}

        return layout;

      }

    }

 

void CreateLayout()

{

                // Something like this anyway

      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);

}

 

I’ve not used OnCreate within Application, it sounds like it would work OK,
but at least with the method above you should get your base layout to be
non-null.

 

Also, I’ve not created/stored any views or layouts within the application
object, and I’ve not worked with activity groups, so I might not be leading
you down the best route!

 

Cheers,

 

Andy

 

From: monodroid-boun...@lists.ximian.com
[mailto:monodroid-boun...@lists.ximian.com] On Behalf Of Narcís Calvet
Sent: 28 July 2011 16:04
To: 'Discussions related to Mono for Android'
Subject: Re: [mono-android] Getting objects from an intent

 

Hi Andy,

 

Thanks for your feedback but I can’t solve the issue using your
recommendations either. Here’s how I implemented the Application:

 

  [Application]

  public class MyApplication : Application

  {

    private static readonly MyApplication theApp = new MyApplication();

    private RelativeLayout layout;

 

    static MyApplication()

    {

    }

 

    public MyApplication(IntPtr handle)

      : base(handle)

    {

    }

 

    public MyApplication()

      : base()

    {

    }

 

    public static MyApplication TheApp

    {

      get

      {

        return theApp;

      }

    }

 

    public RelativeLayout Layout

    {

      get

      {

        return layout;

      }

    }

 

    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);

    }

  }

 

And this is how I use it in Activity’s OnCreate override:

 

      SetContentView(MyApplication.TheApp.Layout);

 

Can you see anything wrong in it?

 

 

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

 

 

From: monodroid-boun...@lists.ximian.com
[mailto:monodroid-boun...@lists.ximian.com] On Behalf Of Andrew Sinclair
Sent: dimecres, 27 / juliol / 2011 18:59
To: 'Discussions related to Mono for Android'
Subject: Re: [mono-android] Getting objects from an intent

 

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 you’ll 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 isn’t 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 “OnCreate”d by Android without you being able to have a stack like
you do on Windows with ShowDialog. Hence if your “current” activity is
“OnDestroy”d by the OS because of, say, memory issues then the next you’ll
know about it will be an “OnCreate” and, quite possibly, your MyApplication
constructor will get called again at that point, And you’ll 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 can’t rely on any references
to those other activities.)

 

Anyway, that’s my understanding at least. These are all things that were a
culture shock to me as a Windows programmer, maybe they’re 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 I’m 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 can’t 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 doesn’t 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 there’s a very good chance that the
“target” activity will be invalid.

 

This might be different with activity groups, I haven’t used them, but in
general I’ve 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 you’d access this when you returned to the original activity. With
Android that’s 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 you’re 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 what’s 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 haven’t 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

Reply via email to