Home > Enterprise >  MAUI Pass data from AppCompatActivity
MAUI Pass data from AppCompatActivity

Time:01-06

First time diving into Mobile app dev on .NET(MAUI) and have a couple of questions regarding "How to do" and "What is the best solution".

Q1 - I eventually implemented Google sign-in in MAUI application through dependency injection. I have GoogleLoginActivity(AppCompatActivity) on the Android platform that is getting data from Google API and manually deserializing it to a custom User class object.

Activity call from GoogleAuthService(android platform):

Platform.CurrentActivity.StartActivity(typeof(GoogleLoginActivity));

Platform injection:

builder.Services.AddSingleton<IGoogleAuthService, GoogleAuthService>();

So the next task is to pass the User object from Android Activity to MAUI MainPage ViewModel or even to any other page in project (LoginConforomation page for example). So, What are my options?

App deserialize GoogleSignInAccount object in override OnActivityResult, and call Activity.Finish()

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            if (requestCode == Constants.RC_SIGN_IN)
            {
                Task<GoogleSignInAccount> task = GoogleSignIn.GetSignedInAccountFromIntentAsync(data);
                HandleResult(task);
            }
        }
        private void googleSignIn()
        {
           ....
        }
        private async void HandleResult(Task<GoogleSignInAccount> result)
        {  
                ....
                Finish();
        }

I've been trying shell navigation (Shell.Current.GoToAsync("MainPage")) with an object in QuerryProperty but it doesn't work (there is no QuerryProperty in my code example).

 protected override void OnDestroy()
            {
                base.OnDestroy();
                try
                {
                    Shell.Current.GoToAsync("MainPage");    
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex);
                    Shell.Current.DisplayAlert("error", ex.Message, "error");
                }
            }

Also as an option is serialize entire object to preferences, but I think it is a bad coding style and not sure that it gonna work.

Q2. Suppose, I have a signed in user and I need periodically keep and check some user data in my app.. What is the best approach to keep user data(ID, GUid, UserName etc) in the applicaion. I imagine smth like sessions in WebForms, but as fas as i know there is no sessions in MAUI. Cookies(is there cookies in the mobile app?)? Preferences(User object is too complex?)? Other options? Thank you.

UPD: Silly me. Question 1 solved I did a rookie mistake and forget to change in App.xaml.cs:

MainPage = new NavigationPage(new MainPage());

to

MainPage = new AppShell();

So even from AppCompatActivity i can do

 Shell.Current.GoToAsync("PostGoogleSignin", true,
             new Dictionary<string, object>
             {
                { "User", resultUser }
             });

and get user on the other side. Thank you for answers.

CodePudding user response:

You can pass the whole object/class as a navigation parameter.

MyObjectClass MyObject = new();

var navigationParameter = new Dictionary<string, object>
{
     { "MyObject", MyObject }
};
await Shell.Current.GoToAsync($"/mypage", navigationParameter);

Then at your (mypage) code behind you will get the object into a new property myobject:

[QueryProperty(nameof(MyObjectClass), "MyObjectNameString")]
public partial class GroupMatchPage : ContentPage
{

    MyObjectClass myobject;
    public MyObject MyObject
    {
        get => myobject;
        set
        {
            myobject = value;
        }
    }
}

As per your 2nd question, you can save the user's data into a simple json file and track all the process as needed.

Hope that helps.

  • Related