Activity does not open

0

Well, I created two Activity's when I clicked on the first button and it should open the second with information received from the first one. But is not that what happens, where did I go wrong?

 using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
//Atividade Principal
namespace TabelaPeriodica
{
    [Activity (Label = "TabelaPeriodica", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {


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

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            EditText edt = FindViewById<EditText> (Resource.Id.edtsmb);
            Button bttn = FindViewById<Button> (Resource.Id.env);


            if (!string.IsNullOrEmpty(edt.Text))
            {
                bttn.Click += delegate {
                    var ress = new Intent (this, typeof(Result));
                    ress.PutExtra ("simbol", edt.Text);
                    StartActivity (ress);
                };


            }


        }

    }
}

Second Activity:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace TabelaPeriodica
{
    [Activity (Label = "Result")]           
    public class Result : Activity
    {

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

            // Create your application here
            SetContentView (Resource.Layout.Result);

            TextView sm = FindViewById<TextView> (Resource.Id.tsimb);

            sm.Text = Intent.GetStringExtra ("simbol") ?? "Erro";

        }
    }
}
    
asked by anonymous 04.08.2014 / 03:43

1 answer

2

See that in OnCreate of your MainActivity you are never adding the event handler of the button click

if (!string.IsNullOrEmpty(edt.Text))
{
    bttn.Click += delegate {
        var ress = new Intent (this, typeof(Result));
        ress.PutExtra ("simbol", edt.Text);
        StartActivity (ress);
    };
}

The OnCreate , as the name itself says is only called when the activity is created, and when it is created, if (!string.IsNullOrEmpty(edt.Text)) will always return false.

You need to change this check, if it should only show the second activity when the user enters a value then the if must be inside this delegate, something like

bttn.Click += delegate {
    if (!string.IsNullOrEmpty(edt.Text))
    {
        var ress = new Intent (this, typeof(Result));
        ress.PutExtra ("simbol", edt.Text);
        StartActivity (ress);
    }
};
    
04.08.2014 / 14:37