Search code examples
xamarin.android

What to write in WriteToParcel method for Datetime type


in xamarin android i have a class

 class Classdaysun 
    {
     public int daysun  { get; set; }
     public int monthsun { get; set; }
     public DateTime time { get; set; }        
   }

and

 public class UserParcelable : Java.Lang.Object, IParcelable

     public void WriteToParcel(Parcel dest, [GeneratedEnum] ParcelableWriteFlags flags)
 {
     dest.WriteInt(classdaysun.daysun);
     dest.WriteInt(classdaysun.monthsun);
     dest.WriteDatetime(classdaysun.time);
     
 }

dest.WriteDatetime;
is not recognized ! Is there a solution ? Thanks


Solution

  • I would suspect the reason WriteDatetime is failing is because you are passing it a C# DateTime. Understandably WriteDatetime doesn’t know how to handle the C# DateTime.

    I’d like to suggest that you take a different approach and eliminate the need to pass the list as you outlined in How to pass generic list to activity on the Microsoft forum.

    Originally you were trying to pass a Bundle. But a Bundle can only handle simple types like strings, ints etc. The suggestion of a parcelable while legitimate android java code is not ideal when working with C#. You could possibly fix it by converting the C# DateTime to the Java equivalent, but that doesn’t solve your real problem.

    Why the need to pass the List in the first place? You are obviously using multiple activities and therefore need to pass the list to another activity. A few years back (2019-20) Android introduced their new NavigationComponent. The major feature of the NavigationComponent was to eliminate multiple activities. Android now recommend only a Single Activity (MainActivity) app combined with multiple fragments representing each screen. This applies equally to Views and Compose. Around the same time, they also introduced ConstraintLayout which simplifies all the problems associated with xml layouts such as RelativeLayout, LinearLayout etc.

    So back to your problem with the List. Now using the NavigationComponent you would instantiate your list in a fragment in the OnCreate() or OnCreateView() of the fragment where you want to use the data in the List, no need to pass it anywhere. Ironically you could even create your List in the MainActivity and refer to it in the fragment. There is now only one activity, therefore you could access it safely by casting it as ((MainActivity)Activity!).MyList. I’m not recommending that, because there shouldn’t be any need, but it is legitimate code.

    The NavigationComponent was introduced with an Android codeLab called NavigationCodeLab. On my GitHub, you will find a converted Xamarin.Android version of their NavigationCodeLab. That is a good starting point to learn how to code Single Activity apps in line with Android’s recommendation of Modern Android Development. Once you get an understanding of the code lab, you will find multiple projects (all relating to Xamarin.Android development using the NavigationComponent). The projects start with NavigationGraph1 and an extra digit is added to the end of the project name for each succeeding project, each project introducing new complexity. They also include Net6, Net7 and Net8 conversions of earlier versions.
    https://github.com/gmck