How do I pass a DateTime variable from one activity to another activity?

I receive a token expiry from a web service on my login activity. I want to use the token expiry on my dashboard activity. The PutExtra() does not accommodate the DateTime variable as a parameter. I tried Storing the date as a string and object from quite a couple of example on the net but I

Login.cs
if (result.ResultField=="Success")
{
Toast.MakeText(this, "Login Success. ", ToastLength.Long).Show();
result.ToString();
Intent intent = new Intent(this, typeof(DashboardView));
//////pass username to welcome message on dashboard
intent.PutExtra("welcomeUser", loginRequest.UserNameField);
intent.PutExtra("token", result.TokenField.TokenField);

                //DateTime TokenExpiryField = result.TokenExpiryField.TokenExpiry;
                //TokenExpiryField.ToString("2017-10-26 18:35:11.890");
                //intent.PutExtra("tokenExpiry", TokenExpiryField.ToString());
                //intent.PutExtra("tokenExpiry", JsonConvert.SerializeObject(result.TokenExpiryField));
                Toast.MakeText(this, "Login pass. ", ToastLength.Long).Show();
                StartActivity(intent);
            }
            else
            {
                Toast.MakeText(this, "Login failed. ", ToastLength.Long).Show();
            }

Dashboard.cs

//token
string TokenField = Intent.GetStringExtra("token");
string TokenExpiry = Intent.GetStringExtra("tokenExpiry");
//GetDashBoardValuesResponse TokenExpiry = (GetDashBoardValuesResponse)Intent.GetSerializableExtra("tokenExpiry");

        //DateTime tokenEx = DateTime.ParseExact(TokenExpiry, "yyyy-MM-dd HH:mm:ss", null);
        //DateTime no = DateTime.Today;
        //TokenExpiry = no.ToString("O");
        //DateTime no2 = DateTime.ParseExact(TokenExpiry, "O", CultureInfo.InvariantCulture);

        //
        welcome = FindViewById<TextView>(Resource.Id.textViewWel);
        string welcomeUser = Intent.GetStringExtra("welcomeUser") ?? "[username]";

        welcome.Text = "SignFlow Mobile App welcomes you " + welcomeUser + " TOKEN IS: " + TokenField + "EXPIRY IS: " +TokenExpiry;

        pending = FindViewById<Button>(Resource.Id.pendingB);
        pending.Click += delegate
        {
            ProgressDialog progress = new ProgressDialog(this);
            progress.Indeterminate = true;
            progress.SetProgressStyle(ProgressDialogStyle.Spinner);
            progress.SetMessage("Logging into SignFlow. Please wait...");
            progress.SetCancelable(true);
            progress.Show();
            GetDashValues(TokenField,PassMyDateHere);

        };
        completed = FindViewById<Button>(Resource.Id.completedB);
    }

    //the method that needs the date parameter
    public async void GetDashValues(string _token,DateTime _tokenExpiry)
    {
        var dashRequest = new Token
        {
            TokenField = _token,
            TokenExpiry = _tokenExpiry
        };
        //using the api
        var result = await App.RestService.GetDashPost(dashRequest.TokenField, dashRequest.TokenExpiry);
        if (result.ResultField=="Success")
        {

            Toast.MakeText(this, "Youre safe for now. ", ToastLength.Long).Show();

        }
        else
        {
            Toast.MakeText(this, "Not this time ", ToastLength.Long).Show();
        }
    }
}

Best Answers

Answers

  • iWrytCodeiWrytCode USMember ✭✭

    I tried the code above. I have used it before. But I don't get an exception or error. But the date appears as"0001-01-01T00:00:00"

  • TedRogersTedRogers USMember ✭✭✭✭

    @iWrytCode sounds like something else is your problem. Your date would appear that way if DateTime is value is set to zero. Can you show the output of the string and DateTime when creating your intent and when extracting date from the intent?

    On intent creation side:
    Console.WriteLine($"strDate = {strDate} now1 = {now1} now1 ticks = {now1.Ticks}");

    In the new activity when extracting the date info:
    Console.WriteLine($"strDate = {strDate} now2 = {now2} now2 ticks = {now2.Ticks}");

  • iWrytCodeiWrytCode USMember ✭✭

    Please find the attached output

    xam.JPG 24.9K
  • TedRogersTedRogers USMember ✭✭✭✭

    @iWrytCode that is not enough info to help you. If you can provide the info I asked for when creating the intent and when extracting the data from the intent I might be able to help.

  • iWrytCodeiWrytCode USMember ✭✭

    Login.cs - Where the intent has been created

    //---------------------------------
    if (result.ResultField == "Success")
    {
    Intent intent = new Intent(this, typeof(DashboardView));
    //pass username to welcome message on dashboard
    intent.PutExtra("welcomeUser", loginRequest.UserNameField);
    intent.PutExtra("token", result.TokenField.TokenField);
    try
    {
    intent.PutExtra("expiry", result.TokenExpiryField.TokenExpiry.ToString());
    //intent.PutExtra("expiry", Convert.ToString(result.TokenExpiryField.TokenExpiry));
    }
    catch (Exception e)
    {

                        e.Message.ToString();
                    }
                    StartActivity(intent);
    
                }
                else
                {
                    Toast.MakeText(this, "Login failed. ", ToastLength.Long).Show();
                }
    
            }
    

    Dashboard.cs - Where the intent has been extracted

    //---------------------------------
    base.OnCreate(bundle);
    //creating my applicationright here
    SetContentView(LayoutResource);
    //token
    string tokenField = Intent.GetStringExtra("token");
    //expiry
    string tokenExpiry = Intent.GetStringExtra("expiry");

                //-------------try Ted's approach code
                const string FMT = "O";
                DateTime conv = Convert.ToDateTime(tokenExpiry);
                string strDate = conv.ToString(FMT);
                DateTime convExpiry = DateTime.ParseExact(strDate, FMT, CultureInfo.InvariantCulture,           DateTimeStyles.RoundtripKind);
                convExpiry.ToBinary();
    

    Then I must use both token and token expiry to post and get results from an API. tokenField is a string and it works. But the tokenExpiry is a DateTime datatype. The API method is standard and it works with static variables.

  • JohnHJohnH GBMember ✭✭✭✭✭

    @iWrytCode Don't know if this will help, but I simply transfer dates between intents by the ticks.

  • iWrytCodeiWrytCode USMember ✭✭

    @JohnHair Ohkaaay? How do you do that?

  • robbitrobbit CNMember Xamurai

    In the MainActivity use this to slide to the Activity1

    Intent intent = new Intent(this,typeof(Activity1));
    intent.PutExtra("time", DateTime.Now.ToString());
    StartActivity(intent);
    

    In the Activity1's onCreate() method

    string time=Intent.GetStringExtra("time");
    DateTime dt= Convert.ToDateTime(time);
    System.Diagnostics.Debug.Write("1111111 "+time);
    System.Diagnostics.Debug.Write(dt.ToString());
    

    this is my output result:
    [0:] 1111111 2017/10/31 9:34:17
    [0:] 2017/10/31 9:34:17

Sign In or Register to comment.