Monodroid GREF problem best practice?

前端 未结 3 1710
醉话见心
醉话见心 2021-01-27 03:51

I have the following test code (based on standard monodroid HelloWorld)

namespace TestGREF
{
    [Activity (Label = \"TestGREF\", MainLauncher = true)]
    publi         


        
3条回答
  •  我在风中等你
    2021-01-27 04:12

    You need to release all unmanaged objects when they no longer needed. All classes that inherits from Android.Runtime.IJavaObject also inherits IDisposable so you need to dispose them.

    Here is part from my project

    private Spinner _spType;
    private ArrayAdapter _arrayAdapter;
    
    protected override void OnCreate(Android.OS.Bundle savedInstanceState)
    {
      base.OnCreate(savedInstanceState);
      _spType = FindViewById(Resource.Id.spinnerType);
      _arrayAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, new[] {"1","2","3","4","5"});
      _spType.Adapter = _arrayAdapter;
    }
    
    public override void Finish()
    {
        if (_spType != null)
            _spType.Dispose();
        if (_arrayAdapter != null)
            _arrayAdapter.Dispose();
        base.Finish();
    }
    

提交回复
热议问题