Friday, November 22, 2013

Android: Switch Between Activities

After I mentioned a couple times in the previous posts, that Android applications are made up out of activities (just like Windows Forms applications are of forms), I want to show today, how a new activity can be added to the application, how this can be started and how data can be transfered.
We add a new activity via File - New - Android Activity, I call it here "SecondActivity.cs".
Of course we can now work with it analogously like with the main activity.
So we can create a layout via code, or via an axml file. I here choose the second option, for that we right click on Resources - layout and select Add - New File - Android Layout. I name the layout "Second".
From the main activity we then can start the second activity as follows:

var SecondActivity = new Intent(this, typeof(SecondActivity));
StartActivity(SecondActivity);

Optionally we can pass over string parameters when starting:

SecondActivity.PutExtra("Parameter", "Nachricht von Main");

The complete code of the file "MainActivity.cs" looks like this:

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

namespace MultipleActivities
{
     [Activity (Label = "MultipleActivities", MainLauncher = true)]
     public class MainActivity : Activity
     {
          protected override void OnCreate (Bundle bundle)
          {
               base.OnCreate (bundle);

               var Layout = new LinearLayout (this);
               Layout.Orientation = Orientation.Vertical;
               var Button1 = new Button (this);
               Button1.Text = "Second starten";

               Button1.Click += delegate {
                    var SecondActivity = new Intent(this, typeof(SecondActivity));
                    SecondActivity.PutExtra("Parameter", "Nachricht von Main");
                    StartActivity(SecondActivity);
               };

               Layout.AddView (Button1);

               SetContentView (Layout);
          }
     }
}

So when clicking the button, the second activity is started.
This only contains a textview, the corresponding layout code of the file "Second.axml" is:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <TextView
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" />
</LinearLayout>

The code of the activity "SecondActivity.cs" is also pretty short, in it just the parameter passed over by the main application is read-out and displayed in the textview:

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 MultipleActivities
{
     [Activity (Label = "SecondActivity")]              
     public class SecondActivity : Activity
     {
          protected override void OnCreate (Bundle bundle)
          {
               base.OnCreate (bundle);
               SetContentView (Resource.Layout.Second);

               TextView textView1 = FindViewById (Resource.Id.textView1);
               textView1.Text = Intent.GetStringExtra ("Parameter");
          }
     }
}

No comments:

Post a Comment