Xamarin Tutorials
In this example we will examine how to multiple activities communicate each other.
Android application is composed of multiple activities and they often communicate with each other by passing data. For example, lets say your application contains two activity Activity1 and Activity2. Activity1 is marked as launcher activity, which means it will be invoked automatically when user taps on the application icon from applications list. Note that, there can be at most one launcher activity throughout application. When user clicks on a button in Activity1, it will invoke Activity2.

Starting activity in Xamarin.Android

Xamarin Android provides easy to use startActivity method in activity context. The following code snippet will start Activity2 without passing any data
 StartActivity(typeof(Activity2));

Starting activity by passing arguments

The above code block starts Activity2 without passing any data. However Xamarin android allows you to pass data from one activity to another using Intent bundle. All you have to do is to pass the instance of Intent to startActivity() method.
The Intent describes the activity to start and carries the data bundle to be sent to target activity. Intent can carry data types as key-value pairs called extras. The putExtra()method takes the key name in the first parameter and the value in the second parameter. Passing data between activities is limited to primitive data types. For object type you must use Parceable or Serilizable. For more information on the data types and intent, you can refer to official android documentation from below links.
http://developer.android.com/reference/android/content/Intent.html
http://developer.android.com/reference/android/os/Bundle.html
Intent intent = new Intent(this, typeof(MainActivity2));
intent.PutExtra("name", "Nilanchala");
intent.PutExtra("empid", 1001);
StartActivity(intent);
Retrieving data bundles received
We can receive the above passed data in “Activity2” using the below code snippet
string name = Intent.GetStringExtra("name");
int roll = Intent.GetIntegerExtra("empid");
Note that you must use the same keys such as “name” and “empid” while retrieving the values from Activity2.

Receiving result back from Activity

If you want to receive a result from the activity when it finishes, callstartActivityForResult() instead of startActivity(). Your activity receives the result as a separate Intent object in your activity’s onActivityResult() callback.
Source: javatechig
Axact

Author

My name is Dave, Am a cool IT Geek, computer analyst and a tutor. I do alot of computer stuffs like programming, web development, blogging, data administrator, computer security and lots more. Feel free to contact me if want more informations and tutorials.

Post A Comment:

0 comments: