0

I have implemented a countdown class in my app and i intend to close this app when the OnFinish() method in the class is called in OnCreate after future parameter expires. I tried to create an instance of my activity class and then the method FinishAffinity but it didnt work, Here is my code...

 public class MainActivity : AppCompatActivity
    {
        private GLSurfaceView glView;
        public static Android.App.AlertDialog.Builder builder;
        public static bool g = false;

        protected override void OnCreate(Bundle savedInstanceState)
        {
         MyTimer myTimer = new MyTimer(10000, 1000);
            myTimer.Start();
            myTimer.OnTick(1000);
            myTimer.OnFinish();
        }
    }
  public class MyTimer : CountDownTimer
    {
        private int a = 10;

        public MyTimer(long future, long interval) : base(future, interval)
        {
        }

        public override void OnFinish()
        {
       //There is an error for accessing the method directly so i tried instantiating the class and then calling the Finish Method
        MainActivity y= new MainActivity();
        //Call method to finish all underlying activities
          y.FinishAffinity();
        }

        public override void OnTick(long millisUntilFinished)
        {
            int u = a--;
            MyToast.MakeText(Application.Context, "this app will close in " + u + " " + "seconds", ToastLength.Short).Show();
        }
    }

The FinishAffinity method in the OnFinish method is not working so is there any way to call this from outside the class without creating a static to non-static reference error

2 Answers2

0

You are creating a new instance of the MainActivity in the onFinish()
i.e. MainActivity y = new MainActivity(); which is not right.
It should be simply MainActivity.this.finishAffinity() if the Countdown class is a nested inner class.

Otherwise, you can simple create a MainActivity parameter in your Countdown class' constructor & call mainActivity.finishAffinity()

Darshan
  • 4,020
  • 2
  • 18
  • 49
  • The first solution you suggested is throwing a null object reference and then for the second, the app freezes on launch, is there something like ```(MainActivity)GetActivity)```kind of thing i can use –  Mar 02 '21 at 08:21
  • I don't have much experience in `C#`, but I guess you can check a basic implementation of a CountDown here: https://stackoverflow.com/a/10032406/6819340 – Darshan Mar 02 '21 at 08:24
  • Okay thanks for the advise anyways, am leaving an upvote –  Mar 02 '21 at 08:26
  • Can i create an intent and send to a broadcast receiver which i will then use to close the app? –  Mar 02 '21 at 08:52
0

Declare your programid in the class and make it static so you can access it from the CountDownTimer class and then call killprocess(int y) like this

 public class MainActivity : AppCompatActivity
    {
        private GLSurfaceView glView;
        public static Android.App.AlertDialog.Builder builder;
        public static bool g = false;
        public static int ourProgramid;

        protected override void OnCreate(Bundle savedInstanceState)
        {
         MyTimer myTimer = new MyTimer(10000, 1000);
            myTimer.Start();
            myTimer.OnTick(1000);
            myTimer.OnFinish();
            ourProgramid=Android.OS.Process.MyId();
        }
    }
  public class MyTimer : CountDownTimer
    {
        private int a = 10;

        public MyTimer(long future, long interval) : base(future, interval)
        {
        }

        public override void OnFinish()
        {
       //There is an error for accessing the method directly so i tried instantiating the class and then calling the Finish Method
       MainActivity y= new MainActivity();
        //Use killprocess to exit app instead
          Android.Os.Process.KillProcess(MainActivity.ourProgramId);
        }

        public override void OnTick(long millisUntilFinished)
        {
            int u = a--;
            MyToast.MakeText(Application.Context, "this app will close in " + u + " " + "seconds", ToastLength.Short).Show();
        }
    }