Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Share data between Activity

Good morning,
i'm a newbie regarding xamarin and android software , my intention is to create an app that connect to a TCPServer (a computer ) just to send some command so the software in the pc when receive the string with the command will do something. Now i already search in the forum and i couldnt find some example or info that allow me to do that ,

`namespace TCPClient
{
[Activity(Label = "TCPClient", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{

TcpClient client; 
NetworkStream stream; 
byte[] datalength = new byte[8]; 


protected override void OnCreate(Bundle bundle)
{
  base.OnCreate(bundle);
  // Set our view from the "main" layout resource
  SetContentView (Resource.Layout.Main);
  Button LogIn = FindViewById<Button>(Resource.Id.ButtonLogIn);
  LogIn.Click += delegate
  {
    var intent = new Intent(this, typeof(LoginActivity));        
    StartActivity(intent);
  };
}

}
}`

i would like to test (and if u give me some hint also to how to test a connection without error) if i can connect to the tcpserver and if i can to connect to that server , and to get access to "TcpClient client;" also in the other activity so i can send the command from another activity if you need any other info just ask me , thanks a lot for your help

Posts

  • JustinWhelanJustinWhelan NZMember

    Hi

    I don't know if it's the only way however I normally use the below method:

            button.Click += delegate
            {
                var OrderPage = new Intent(this, typeof(OrderPage));
                OrderPage.PutExtra("Place", Place.Text);
                OrderPage.PutExtra("Delivery", Delivery.Text);
                OrderPage.PutExtra("Order", Order.Text);
                OrderPage.PutExtra("Number", Number.Text);
                OrderPage.PutExtra("Requests", Requests.Text);
                StartActivity(OrderPage);
            };
    

    then on the receiving activity

            string delivery = Intent.GetStringExtra("Delivery");
            string order = Intent.GetStringExtra("Order");
            string requests = Intent.GetStringExtra("Requests");
            string number = Intent.GetStringExtra("Number");
            string place = Intent.GetStringExtra("Place") ?? "Data not available";
    

    Hope this helps.

  • IppocampoIppocampo ITMember
    edited March 2017

    @JustinWhelan said:
    Hi

    I don't know if it's the only way however I normally use the below method:

            button.Click += delegate
            {
                var OrderPage = new Intent(this, typeof(OrderPage));
                OrderPage.PutExtra("Place", Place.Text);
                OrderPage.PutExtra("Delivery", Delivery.Text);
                OrderPage.PutExtra("Order", Order.Text);
                OrderPage.PutExtra("Number", Number.Text);
                OrderPage.PutExtra("Requests", Requests.Text);
                StartActivity(OrderPage);
            };
    

    then on the receiving activity

            string delivery = Intent.GetStringExtra("Delivery");
            string order = Intent.GetStringExtra("Order");
            string requests = Intent.GetStringExtra("Requests");
            string number = Intent.GetStringExtra("Number");
            string place = Intent.GetStringExtra("Place") ?? "Data not available";
    

    Hope this helps.

    First , thank you for your answer but i'm sorry i have to reply with another question :) , with that method if i'm not wrong i sent to the other activity a string and not the tcpclient so in the new activity i have to declare a new tcpclient ? and open again the connection ? because i read that the tcpclient connection get closed if i switch activities ,

    best regards

  • LjusnanLjusnan DEMember ✭✭✭

    @Ippocampo You could make the TcpClient public static, so every activity can access the client by MainActivity.Client

  • IppocampoIppocampo ITMember
    edited March 2017

    @Ljusnan said:
    @Ippocampo You could make the TcpClient public static, so every activity can access the client by MainActivity.Client

    @Ljusnan Hi thank you for your answer , probably im doing it in the wrong way , because i can't accesso to the Client where i have to declare it ?

    `namespace TCPClient
    {
    [Activity(Label = "TCPClient", MainLauncher = true, Icon = "@drawable/icon")]

    public class MainActivity : Activity
    {
    public static TcpClient Connection = new TcpClient();

    protected override void OnCreate(Bundle bundle)
    {
    
      base.OnCreate(bundle);
      SetContentView(Resource.Layout.Main);
      Button Start = FindViewById<Button>(Resource.Id.StartButton);
    
      Start.Click += delegate
      {
        var intent = new Intent(this, typeof(LoginActivity));
        StartActivity(intent);
      };
    }
    

    }
    }`

    In the login activity i cant find MainActivity.Connection

    EDIT : Seems that now i can see the tcpclient ... i don't know why but seems it's working right now THANK YOU !! both of you
    Anyway last quesiton, to "mantain" the connection i have to connect everytime for each activity right ? everytime i switch activity the first thing to do is connection.connect right ?

  • LjusnanLjusnan DEMember ✭✭✭

    @Ippocampo said:
    EDIT : Seems that now i can see the tcpclient ... i don't know why but seems it's working right now THANK YOU !! both of you
    Anyway last quesiton, to "mantain" the connection i have to connect everytime for each activity right ? everytime i switch activity the first thing to do is connection.connect right ?

    No, you only need to connect once, because the tcpclient stays the same object.

  • IppocampoIppocampo ITMember

    @Ljusnan thank you for your help than i just open the connection in the main activity and check if everything works correctly , i really appreciate your answers .

    Have a nice day :)

  • LjusnanLjusnan DEMember ✭✭✭

    @Ippocampo said:
    @Ljusnan thank you for your help than i just open the connection in the main activity and check if everything works correctly , i really appreciate your answers .

    Have a nice day :)

    No problem! And thanks, have a nice day too.

Sign In or Register to comment.