Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 157f681

Browse files
samhoutsrmarinho
authored andcommitted
[Android] Dispose check before setting properties on Button (#1013)
1 parent 2961f3a commit 157f681

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

Xamarin.Forms.Platform.Android/FastRenderers/ButtonRenderer.cs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public class ButtonRenderer : AppCompatButton, IVisualElementRenderer, AView.IOn
2121
Typeface _defaultTypeface;
2222
int _imageHeight = -1;
2323
bool _isDisposed;
24-
bool _inputTransparent;
25-
readonly Lazy<TextColorSwitcher> _textColorSwitcher;
26-
readonly AutomationPropertiesProvider _automationPropertiesProvider;
24+
bool _inputTransparent;
25+
readonly Lazy<TextColorSwitcher> _textColorSwitcher;
26+
readonly AutomationPropertiesProvider _automationPropertiesProvider;
2727
readonly EffectControlProvider _effectControlProvider;
2828
VisualElementTracker _tracker;
2929
ButtonBackgroundTracker _backgroundTracker;
@@ -33,9 +33,9 @@ public class ButtonRenderer : AppCompatButton, IVisualElementRenderer, AView.IOn
3333

3434
public ButtonRenderer() : base(Forms.Context)
3535
{
36-
_automationPropertiesProvider = new AutomationPropertiesProvider(this);
36+
_automationPropertiesProvider = new AutomationPropertiesProvider(this);
3737
_effectControlProvider = new EffectControlProvider(this);
38-
_textColorSwitcher = new Lazy<TextColorSwitcher>(() => new TextColorSwitcher(TextColors));
38+
_textColorSwitcher = new Lazy<TextColorSwitcher>(() => new TextColorSwitcher(TextColors));
3939

4040
Initialize();
4141
}
@@ -145,7 +145,7 @@ void IVisualElementRenderer.SetElement(VisualElement element)
145145
SendVisualElementInitialized(element, this);
146146

147147
EffectUtilities.RegisterEffectControlProvider(this, oldElement, element);
148-
148+
149149
Performance.Stop();
150150
}
151151

@@ -185,7 +185,7 @@ protected override void Dispose(bool disposing)
185185
_tracker?.Dispose();
186186

187187
_backgroundTracker?.Dispose();
188-
188+
189189
if (Element != null)
190190
{
191191
Element.PropertyChanged -= OnElementPropertyChanged;
@@ -195,15 +195,15 @@ protected override void Dispose(bool disposing)
195195
base.Dispose(disposing);
196196
}
197197

198-
public override bool OnTouchEvent(MotionEvent e)
199-
{
200-
if (!Enabled || (_inputTransparent && Enabled))
201-
return false;
198+
public override bool OnTouchEvent(MotionEvent e)
199+
{
200+
if (!Enabled || (_inputTransparent && Enabled))
201+
return false;
202202

203-
return base.OnTouchEvent(e);
204-
}
203+
return base.OnTouchEvent(e);
204+
}
205205

206-
protected virtual Size MinimumSize()
206+
protected virtual Size MinimumSize()
207207
{
208208
return new Size();
209209
}
@@ -267,7 +267,7 @@ protected virtual void OnElementPropertyChanged(object sender, PropertyChangedEv
267267

268268
protected override void OnLayout(bool changed, int l, int t, int r, int b)
269269
{
270-
if (Element == null)
270+
if (Element == null || _isDisposed)
271271
{
272272
return;
273273
}
@@ -315,12 +315,12 @@ void Initialize()
315315
AddOnAttachStateChangeListener(this);
316316
OnFocusChangeListener = this;
317317

318-
Tag = this;
319-
}
318+
Tag = this;
319+
}
320320

321321
void UpdateBitmap()
322322
{
323-
if (Element == null)
323+
if (Element == null || _isDisposed)
324324
{
325325
return;
326326
}
@@ -381,7 +381,7 @@ void UpdateBitmap()
381381

382382
void UpdateFont()
383383
{
384-
if (Element == null)
384+
if (Element == null || _isDisposed)
385385
{
386386
return;
387387
}
@@ -413,17 +413,27 @@ void UpdateFont()
413413

414414
void UpdateIsEnabled()
415415
{
416+
if (Element == null || _isDisposed)
417+
{
418+
return;
419+
}
420+
416421
Enabled = Element.IsEnabled;
417422
}
418423

419-
void UpdateInputTransparent()
420-
{
421-
_inputTransparent = Element.InputTransparent;
422-
}
424+
void UpdateInputTransparent()
425+
{
426+
if (Element == null || _isDisposed)
427+
{
428+
return;
429+
}
430+
431+
_inputTransparent = Element.InputTransparent;
432+
}
423433

424-
void UpdateText()
434+
void UpdateText()
425435
{
426-
if (Element == null)
436+
if (Element == null || _isDisposed)
427437
{
428438
return;
429439
}
@@ -440,7 +450,7 @@ void UpdateText()
440450

441451
void UpdateTextColor()
442452
{
443-
if (Element == null)
453+
if (Element == null || _isDisposed)
444454
{
445455
return;
446456
}
@@ -450,7 +460,7 @@ void UpdateTextColor()
450460

451461
void UpdateDrawable()
452462
{
453-
_backgroundTracker.UpdateDrawable();
463+
_backgroundTracker?.UpdateDrawable();
454464
}
455465

456466
}

0 commit comments

Comments
 (0)