devielu
devielu

Reputation: 198

How to this without repeating code? Xamarin - Android

i don't want to repeat this code all times:

Button btnClicks = FindViewById<Button>(Resource.Id.MyButton);
Button btnWarning = FindViewById<Button>(Resource.Id.ButtonWarning);

How i can improve this code to look more clean? This spam of code can really affect the performance of the application?

 protected override void OnResume()
    {          
        base.OnResume();
        Button btnClicks = FindViewById<Button>(Resource.Id.MyButton);
        Button btnWarning = FindViewById<Button>(Resource.Id.ButtonWarning);

        btnWarning.Click += btnWarn;
    }

    protected override void OnPause()
    {
        base.OnPause();
        Button btnClicks = FindViewById<Button>(Resource.Id.MyButton);
        Button btnWarning = FindViewById<Button>(Resource.Id.ButtonWarning);

        btnWarning.Click -= btnWarn;

    }

    private void btnWarn(object sender, EventArgs e)
    {
        Button btnWarning = FindViewById<Button>(Resource.Id.ButtonWarning);
        btnWarning.Text = string.Format("Warning Test");
    }

Upvotes: 0

Views: 98

Answers (3)

xleon
xleon

Reputation: 6375

Whenever you subscribe to an event, you should unsubscribe as well to release any memory resources attached to the activity. If you don´t unsubscribe, you may get OutOfMemoryException eventually. Actually, you were doing that part correctly, but as others mentioned, you need to use OnCreate to find views just once:

Button btnClicks;
Button btnWarning; 

protected override void OnCreate(Bundle bundle)
{
   base.OnCreate(bundle);

   btnClicks = FindViewById<Button>(Resource.Id.MyButton);
   btnWarning = FindViewById<Button>(Resource.Id.ButtonWarning); 
}

protected override void OnResume()
{          
    base.OnResume();

    btnWarning.Click += btnWarn;
}

protected override void OnPause()
{
    base.OnPause();

    btnWarning.Click -= btnWarn;
}

Upvotes: 0

daramasala
daramasala

Reputation: 3030

Do you really need to remove the Click handler on pause?

It is usually enough to initialize the handler in OnCreate and that's it.

If you do need to access views more than once, then keep a reference to the view in the Activity class itself:

class MyActivity : Activity
{
  Button myButton;

  protected override void OnCreate(Bundle bundle)
  {
     base.OnCreate(bundle);
     var myButton = FindViewById<Button>(Resource.Id.MyButton);
  }

  protected override void OnPause()
  {
    // do something with myButton
  }
}

Upvotes: 1

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12953

Make the buttons class variables and then in OnCreate:

Button btnClicks;
Button btnWarning; 

protected override void OnCreate(Bundle bundle)
{
   base.OnCreate(bundle);
   btnClicks = FindViewById<Button>(Resource.Id.MyButton);
   btnWarning = FindViewById<Button>(Resource.Id.ButtonWarning); 
   btnWarning.Click += btnWarn;
}

now,

private void btnWarn(object sender, EventArgs e)
{
   btnWarning.Text = string.Format("Warning Test");
}

Upvotes: 1

Related Questions