Forum Xamarin.Android

Returning values from nested listactivity to mainactivity

I may be going about this the wrong way, but it's the only way I can think of it right now.

I have a main activity and two 'child' listactivities (one listactivity is a child of the other).

I have a state listactivity and a location listactivity.

I want to start the state listactivity from the main activity (working), and then based on the state chosen, display the location listactivity (working). The final selected location is all I am interested in passing back to the main activity.

I can get the selected state back to the main activity by using StartActivityForResult, but I can't get the selected location back to main activity.

What is the best way to do this?

Thanks in advance.


  • CheesebaronCheesebaron DKInsider, University mod

    I would suggest stop using Activities and start using fragments and simply use C# events or some kind of messaging system to tell the main Activity what was selected. TinyIoC's TinyMessenger could be one way to do it. Or grab the Messenger from MvvmCross and harness it (see: CrossLight)

    But first of all, get rid of the fatty Activities and replace them with light and lean Fragments.

  • Thanks! I was reading about fragments and thought that would be the way to go, I just hadn't run across any examples yet.

  • CheesebaronCheesebaron DKInsider, University mod

    Now this code is just conceptual but should get you going:

    public class StateListFragment : ListFragment
        public Action<StateItem> StateSelected;
        public override View OnCreateView(LayoutInflater inflater, ViewGroup p1, Bundle p2)
            return inflater.Inflate(Resource.Layout.list, null);
        public override void OnActivityCreated(Bundle p0)
            var adapter = new SampleAdapter(Activity);
            var items = new List<StateItem>();
            for (var i = 0; i<20; i++)
                items.Add(new StateItem { IconRes = Android.Resource.Drawable.IcMenuSearch, Tag = "Sample List" });
            ListAdapter = adapter;
            ListView.ItemClick += (s, e) =>
                if (StateSelected != null)
        public class StateItem
            public string Name { get; set; }
            public int IconRes { get; set; }
        private class SampleAdapter : ArrayAdapter<StateItem>
            public SampleAdapter(Context context)
                : base(context, 0)
            public override View GetView(int position, View convertView, ViewGroup parent)
                if (null == convertView)
                    convertView = LayoutInflater.From(Context).Inflate(Resource.Layout.row, null);
                var icon = convertView.FindViewById<ImageView>(Resource.Id.row_icon);
                var title = convertView.FindViewById<TextView>(Resource.Id.row_title);
                title.Text = GetItem(position).Name;
                return convertView;

    Then in your MainActivity you simply assign an action to do when a SampleItem is selected in the ListView this could be to show a new Fragment by replacing the current shown with something else. Like:

    var stateListFragment = new StateListFragment();
    stateListFragment.ItemSelected = state => {
        var t = SupportFragmentManager.BeginTransaction();
        t.Replace(Resource.Id.content_frame, new CountryListFragment(state));

    Given that your layout looks something like follows for your main activity:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android=""
        android:layout_height="match_parent" />
  • Thanks. This makes a lot of sense. I was able to get the 'child' ListActivity data back to the initial Activity but it does seem a tad slow. I'll give this a try tomorrow.

Sign In or Register to comment.