Today while programming my app,I suddenly came across a strange behavior. I was creating a backspace button for my app but the compiler was just skipping a piece of code. Here is the layout of my main.axml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<EditText
android:text="123456123456123456"
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center|left"
android:background="#ffffff"
android:textSize="20sp"
android:textColor="#000000" />
<TextView
android:text="Text"
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center|left"
android:background="#555555"
android:textSize="20sp"
android:textColor="#ffffff" />
<Button
android:text="Backspace"
android:id="@+id/bksp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#555555"
android:textSize="20sp"
android:textColor="#ffffff" />
</LinearLayout>
And here is my MainActivity.cs content
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Views;
using System.Timers;
namespace App2
{
[Activity(Label = "App2", MainLauncher = true, WindowSoftInputMode = SoftInput.StateAlwaysHidden)]
public class MainActivity : Activity
{
private bool Bksp_isdown = false;
private int intt = 0;
private Timer Bksp_tmr;
private Button Bksp;
private EditText Inputt;
private TextView Resultt;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
this.Bksp_tmr = new Timer(500);
this.Bksp = this.FindViewById<Button>(Resource.Id.bksp);
this.Inputt = this.FindViewById<EditText>(Resource.Id.input);
this.Resultt = this.FindViewById<TextView>(Resource.Id.result);
this.Inputt.ShowSoftInputOnFocus = false;
this.Bksp.Touch += this.B_backspace_Touch;
this.Bksp_tmr.Elapsed += this.Bksp_tmr_Elapsed;
}
private void Bksp_tmr_Elapsed(object sender, ElapsedEventArgs e)
{
this.Resultt.Text = "worked"
+ this.Bksp_isdown.ToString() + this.Inputt.SelectionStart.ToString();
while (true)
{
this.intt = this.intt + 100;
this.Backspace();
}
}
private void B_backspace_Touch(object sender, View.TouchEventArgs e)
{
this.Bksp_isdown = true;
if (e.Event.Action == MotionEventActions.Down)
{
this.Backspace();
this.Bksp_tmr.Start();
}
else if (e.Event.Action == MotionEventActions.Up)
{
this.Bksp_tmr.Stop();
this.Bksp_isdown = false;
}
}
private bool Backspace()
{
intt++;
this.Bksp.Text = intt.ToString();
if (this.Inputt.SelectionStart == 0)
return false;
string str = this.Inputt.Text;
int sel = this.Inputt.SelectionStart;
str = str.Substring(0, sel - 1) + str.Substring(sel);
this.Inputt.Text = str;
this.Inputt.SetSelection(sel - 1);
return true;
}
}
}
When I deployed and run this code in an Android 6 device, I saw a strange behavior. There is a method above named Bksp_tmr_Elapsed which ran in the program partially that the while(true) and the code inside it was fully ignored. I haven't seen such behavior in my entire life. You may ask why I have given a constant Boolean expression in while loop. This was only because the code was not running normally as I can expect an infinite loop till crash of the application, but unfortunately not. Please help!!
Update: I checked the code and got a note that only the first line inside Bksp_tmr_Elapsed method executed in the app. I made some changes to the code of which I am providing the screenshot.
Check here the first two lines in the method Bksp_tmr_Elapsed.
Now we can make a conclusion that obviously the app in the device is the updated one and simply ignoring all other lines of the method. Seems like a hidden return statement the compiler is adding to the second line of the method which I am unaware of or whatsoever I don't know. Please explain this!!!