angleUr
angleUr

Reputation: 893

MAUI - Lifecycle methods no longer work in BaseActivity

I am migrating an app from Xamarin.Android to MAUI. I am getting errors in all of the lifecycle methods. For example:

Error CS0115 'BaseActivity.OnPause()': no suitable method found to override AndroidAppCore (net8.0-android)

In Xamarin.Android I did not get any of these errors in BaseActivity.cs, but I do in MAUI The lifecycle methods work in my LoginActivity in MAUI

Activities/Base/BaseActivity.cs:

namespace Common
{
    public abstract class BaseActivity : ISessionExpired, IPBottomNavBarIcon
    

Activities/Login/LoginActivity.cs:

namespace PApp
{
    [Activity]
    public class PatientLoginActivity : Common.BaseActivity

Question: What am I missing in BaseActivity ?

Upvotes: 0

Views: 575

Answers (2)

Jessie Zhang -MSFT
Jessie Zhang -MSFT

Reputation: 13919

From document App lifecycle,we could know:

.NET Multi-platform App UI (.NET MAUI) apps generally have four execution states: not running, running, deactivated, and stopped. .NET MAUI raises cross-platform lifecycle events on the Window class when an app transitions from the not running state to the running state, the running state to the deactivated state, the deactivated state to the stopped state, the stopped state to the running state, and the stopped state to the not running state.

enter image description here

And the Window class defines the following cross-platform lifecycle events: Created,Activated, Deactivated,Stopped,Resumed,Destroying. These cross-platform events map to different platform events, and the following table shows this mapping:

enter image description here

So, you don't need to redefine the lifecycle at all, and you can use these events as needed.

Upvotes: 1

user2153142
user2153142

Reputation: 804

Your BaseActivity doesn't inherit from any standard Android activity, hence the error.

e.g. should be

using AndroidX.AppCompat.App;

[Activity(Label = "BaseActivity")]

public class BaseActivity : AppCompatActivity {

}

Upvotes: 0

Related Questions