Android App being put in unstable state after backing out of the App

Manish Pradhan 1 Reputation point
2021-03-30T17:33:57.147+00:00

XForms: 4.8.0.1821

In my Android App, I use Message Center to subscribe to certain events on the Main XamarinForms Application Class and on some of the Views. For the events in the Views I Unsubscribe when the View is re-instantiated but I dont unsubscribe the Events on the Main Application Class since its a singleton and as per my understanding when its destroyed all the Message Subs also get destroyed.

I have seen when a user uses the device Back button to exit the app, it leaves the App is a weird limbo. The next time the user opens the app, the app goes thorough the entire stack from MainActivity.OnCreate > LoadApplication > Application.OnStart BUT the MessageCenter Subscriptions from the previous instance of the App is still alive. So now my app Registers MessageCenter subs for the second time leading to all sorts of weird bugs and crashes.

To avoid this I have had to keep a reference of the Application Instance as a member variable in the MainActivity and only instantiate if it is null.

static volatile MobileProApp mfptMobileProApp = null;

protected override void OnCreate(Bundle bundle)
{

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);

            if (mfptMobileProApp == null)
            {
                mfptMobileProApp = new MobileProApp();
            }

            LoadApplication(mfptMobileProApp);

            .........

This way I reuse the Application instance when the User returns after exiting the App using the Back button.

This does not happen if the User just hits the Home button and comes back to the app or terminates the app by swiping it off the Apps list.

I have noticed this also happens when the Battery Saver mode is On and Android kills the app when its sent to the background.

Has anyone seen this happening in their Apps? Is this a know issue?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,377 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,711 Reputation points Microsoft Vendor
    2021-03-31T08:28:57.917+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    Yes, it is necessary to unsubscribe from a message when a class no longer wishes to receive the message.

    This does not happen if the User just hits the Home button and comes back to the app or terminates the app by swiping it off the Apps list.

    I have noticed this also happens when the Battery Saver mode is On and Android kills the app when its sent to the background.

    You can try to detect your app being killed. You can use service to do this.

    Please refer to the following code:

    [Service]  
        public class OnClearFromRecentService : Service  
        {  
            public override IBinder OnBind(Intent intent)  
            {  
                return null;  
            }  
      
            [return: GeneratedEnum]  
            public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)  
            {  
                return base.OnStartCommand(intent, flags, startId);  
            }  
      
            public override void OnDestroy()  
            {  
                base.OnDestroy();  
            }  
      
            public override void OnTaskRemoved(Intent rootIntent)  
            {  
                base.OnTaskRemoved(rootIntent);  
      
                // add your code   
            }  
        }  
    

    Note: And now whenever you clear your app from android recent ,then method onTaskRemoved() will execute.

    In addition,the MessagingCenter helps you keep your code decoupled. Sometimes you will find yourself in a position that requires you create a reference between certain code, but by doing so, you have to compromise on reusability and maintainability.

    So try to use it as a last resort, and usually there should be another way to achieve your desired functionality. While sending a message can be very powerful, using it too much can really eat into your readability.

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.