Back button navigation doesn't work in xamarin android

Shay Wilner 1,746 Reputation points
2024-06-28T12:27:23.36+00:00

Hello

I have a table of buttons each button displays a number

When i click one of them i start a new activity

Then if i click the back button i doesn't return to the precedent activity

i have to click once again the back button. It depends on which buttons i clicked in the table

if i click a button of the first rank i have to click the back button more than once;

using Android.Content;
using Android.Views;
using Android.Widget;
using System.Collections.Generic;
namespace Test.Resources.layout
{
    internal class Adaptertable : BaseAdapter<Classweek1>
    {
        private Context context;
        private List<Classweek1> listdaymonth;
         private int themonth;
        public Adaptertable(Context context, List<Classweek1> lisdays): base()
        {
            this.context = context;
            this.listdaymonth = lisdays;    
        }
        public override Classweek1 this[int position]
        {
            get { return listdaymonth[position]; }
        }
        public override int Count
        {
            get { return listdaymonth.Count; }
        }
        public override long GetItemId(int position)
        {
            return position;
        }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView;
            var item = listdaymonth[position];
            if (view == null)
            { 
               LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
               view = inflater.Inflate(Resource.Layout.tablelayout, null);
            }
            else
            view = convertView;
            var btn1 = view.FindViewById<Button>(Resource.Id.bt1);
            var btn2 = view.FindViewById<Button>(Resource.Id.bt2);
            var btn3 = view.FindViewById<Button>(Resource.Id.bt3);
            var btn4 = view.FindViewById<Button>(Resource.Id.bt4);
            var btn5 = view.FindViewById<Button>(Resource.Id.bt5);
            var btn6 = view.FindViewById<Button>(Resource.Id.bt6);
            var btn7 = view.FindViewById<Button>(Resource.Id.bt7);
            btn1.Text = item.Days[0].ToString();
            btn2.Text = item.Days[1].ToString();
            btn3.Text = item.Days[2].ToString();
            btn4.Text = item.Days[3].ToString();
            btn5.Text = item.Days[4].ToString();
            btn6.Text = item.Days[5].ToString();
            btn7.Text = item.Days[6].ToString();
            btn7.Click += (o, e) =>
            {
              Android.Content.Intent intent = new Android.Content.Intent(context, typeof(Activity2));
              context.StartActivity(intent);
            };
            btn6.Click += (o, e) =>
            {
                Android.Content.Intent intent = new Android.Content.Intent(context, typeof(Activity2));
                context.StartActivity(intent);
            };
            btn5.Click += (o, e) =>
            {
                Android.Content.Intent intent = new Android.Content.Intent(context, typeof(Activity2));
                context.StartActivity(intent);
            };
            btn4.Click += (o, e) =>
            {
                Android.Content.Intent intent = new Android.Content.Intent(context, typeof(Activity2));
                context.StartActivity(intent);
            };
            btn3.Click += (o, e) =>
            {
                Android.Content.Intent intent = new Android.Content.Intent(context, typeof(Activity2));
                context.StartActivity(intent);
            };
            btn2.Click += (o, e) =>
            {
                Android.Content.Intent intent = new Android.Content.Intent(context, typeof(Activity2));
                context.StartActivity(intent);
            };
            btn1.Click += (o, e) =>
            {
                Android.Content.Intent intent = new Android.Content.Intent(context, typeof(Activity2));
                context.StartActivity(intent);
            };
            return view;
        }
        //Fill in cound here, currently 0
    }
    internal class AdaptertableViewHolder : Java.Lang.Object
    {
        //Your adapter views to re-use
        //public TextView Title { get; set; }
    }
}
using System.Collections.Generic;
 namespace Test
{
    class Classweek1
    {
  public List<int> Days { get; set; }  
    }
}


<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/bt7"
        android:textSize="22sp"
        android:textStyle="bold"
        android:fontFamily="calibri"
        android:background="#FFa6c9d4"
        android:textColor="#ffffffff"
       android:layout_columnWeight= "1"
       
        />
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/bt6"
        android:textSize="22sp"
        android:textStyle="bold"
        android:fontFamily="calibri"
        android:textColor="#ffffffff"
       android:background="#FF696ebb"
        android:layout_columnWeight= "1"
        />
        <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/bt5"
        android:textSize="22sp"
        android:textStyle="bold"
        android:fontFamily="calibri"
        android:textColor="#ffffffff"
      
        android:background="#FFa15fa4"   
        android:layout_columnWeight= "1"
       />
        <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/bt4"
        android:textSize="22sp"
        android:textStyle="bold"
        android:fontFamily="calibri"
       
        android:textColor="#ffffffff"
        android:background="#FF8ad4e1"
        android:layout_columnWeight= "1"
        />
        <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/bt3"
        android:textSize="22sp"
        android:textStyle="bold"
        android:fontFamily="calibri"
        android:textColor="#ffffffff"
     
        android:layout_columnWeight= "1"
         android:background="#FF529541"
        />
        <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/bt2"
        android:textSize="22sp"
        android:textStyle="bold"
        android:fontFamily="calibri"
        android:textColor="#ffffffff"
     
        android:layout_columnWeight= "1"
        android:background="#FF835734"    
       />
        <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/bt1"
        android:textSize="22sp"
        android:textStyle="bold"
        android:fontFamily="calibri"
        android:textColor="#ffffffff"
       android:layout_columnWeight= "1"
       android:background="#FF5e6bce"  
        />
</GridLayout>
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Test.Resources.layout;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
    [Activity(MainLauncher = true, ConfigurationChanges = Android.Content.PM.ConfigChanges.ScreenSize, ScreenOrientation = Android.Content.PM.ScreenOrientation.Landscape)]
    public class Activity1 : Activity
    {
        private GridView table1;
        private Adaptertable adt;
        private List<Classweek1> month = new List<Classweek1>();
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.layout1);
            table1 = FindViewById<GridView>(Resource.Id.table1);
            Setdays();
            table1.Adapter = new Adaptertable(this, month);
            // Create your application here
        }
        private void Setdays ()
        {
            Classweek1 week = new Classweek1();
            week.Days = new List<int>();
             
            for (int i  = 1; i <= 35;i++)
            {
                week.Days.Add(i); 
               if (i% 7 == 0)
               {
                    month.Add(week);
                    week = new Classweek1();
                    week.Days = new List<int>();
                }
            }
          }
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <GridView
    android:layout_width= "wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/table1" 
  />
</LinearLayout>



Screenshot_20240628_151224_hebrewcal[1] When i click button 1....7 i start a new activity (activity2)

then if i click the back button it doesn't return to the table i have to click the back button once again .The problem persists with buttons of the first rank.

Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,319 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Shay Wilner 1,746 Reputation points
    2024-06-28T14:34:26.9366667+00:00

    I resolve the bug

    In adaptertable class

    all the code lines in getview

    var btn1 = view.FindViewById<Button>(Resource.Id.bt1); var btn2 = view.FindViewById<Button>(Resource.Id.bt2); var btn3 = view.FindViewById<Button>(Resource.Id.bt3); var btn4 = view.FindViewById<Button>(Resource.Id.bt4); var btn5 = view.FindViewById<Button>(Resource.Id.bt5); var btn6 = view.FindViewById<Button>(Resource.Id.bt6); var btn7 = view.FindViewById<Button>(Resource.Id.bt7); 
    .......
    
    
    

    must be in if (view == null) code block

    0 comments No comments