0

I've found that whilst there are a lot of tutorials on Xamarin Android, there does not seem to be a great deal on how to dispose of resources. More particularly, when they are disposed of.

For example, in the OnCreate handler of an activity, I am making several Rx subscriptions, each of which returns an IDisposable. I have tried to dispose of those in various other handlers (e.g. OnDestroy), but those handlers never get invoked. But the subscriptions seem to pile up because OnCreate runs every time the activity is navigated to.

In addition to those subscriptions, there's all the UI controls (TextViews, Buttons etc.) which I am assigning to class-level variables (fields). And those also implement IDisposable.

For all I know, I've got memory leaks all over the place.

Is there a guidance on this anywhere?

onefootswill
  • 3,707
  • 6
  • 47
  • 101
  • I would advise you to look at the Activity lifecycle flow diagram : https://developer.android.com/guide/components/activities/activity-lifecycle.html The `OnResume` and `OnPause` overrides are your friends ;-) – SushiHangover Dec 19 '17 at 01:10
  • @SushiHangover I'll take a look. I wonder why OnCreate gets invoked if OnPause gets invoked. I would have thought OnResume would mean that OnCreate does not need to run again. But I will take a look at those handlers. – onefootswill Dec 19 '17 at 01:42
  • `OnCreate` would be called **if** the Activity needs to be created, are you are creating a new instance if it instead of just calling `Finish` on the current activity to return the last one in the navigation stack? – SushiHangover Dec 19 '17 at 01:48
  • @SushiHangover I'm not calling Finish. I'll have to check my code when I get home again, but I think I'm calling StartActivity to navigate back to the original Activity. I can tell from your comment I should be popping it from the stack to go back. – onefootswill Dec 19 '17 at 04:16
  • Yes, creating a new instance via `StartActivity` is not ***typically*** what you want to do. There are instances where that is what you want but it is added to the stack. Or adding that the activity is `singleTop` in order to send a new Intent to it to an existing instance (reading that intent within the`OnNewIntent` override), etc... but normally you would just `Finish` the activity you are on and return to the last one.. – SushiHangover Dec 19 '17 at 04:27
  • Could you please post your code ? – York Shen Dec 20 '17 at 01:36

1 Answers1

0

@SushiHangover is correct (thanks Sushi). OnPause and OnResume were the events I was after. I also had a bit of a challenge in that when I clicked my custom "Back to Start" button, I needed to go right back to the start screen (skipping the intermediate screen along the way).

The way to do that is use the ClearTop ActivityFlag (Android.Content.ActivityFlags.ClearTop) when starting the Home screen activity. Raw Android code version of this can be seen here https://stackoverflow.com/a/5794572/540156

When you do that, you can clean things up on the activities which get popped off the back-stack as they get popped (in the OnDestroy handler, from recollection).

onefootswill
  • 3,707
  • 6
  • 47
  • 101