Hafeez Ali

Hafeez Ali

  • NA
  • 28
  • 3.6k

Xamarin android passing and reset time between activities

Dec 1 2018 12:51 PM
Pass time between activity and reset from other activity
 
Need Help Kindly reply fast
 
Hi! Everyone I'm new to xamarin kindly helps me.
 
I want to pass time to SeeTime activity and reset time to 60 from that activity which will also reset time in the MainActivity.
 
Code of MainActivity:
  1. public class MainActivity : AppCompatActivity  
  2. {  
  3. public EditText timer;  
  4. Button btn;  
  5. public int elapsedTime; // counting values integer  
  6. Handler myHandler; // used to delay runnable for a second which delay_RATE  
  7. int delay_RATE = 1000; //delay  
  8. Java.Lang.Runnable r;  
  9. protected override void OnCreate(Bundle savedInstanceState)  
  10. {  
  11. base.OnCreate(savedInstanceState);  
  12. SetContentView(Resource.Layout.activity_main);  
  13. elapsedTime = 60; // default value  
  14. myHandler = new Handler(); //initializing handler  
  15. r = new Java.Lang.Runnable(Run);  
  16. timer = FindViewById(Resource.Id.timertxt);  
  17. btn = FindViewById(Resource.Id.btn1);  
  18. btn.Click += Btn_Click;  
  19. Counter();  
  20. }  
  21.   
  22. private void Btn_Click(object sender, System.EventArgs e)  
  23. {  
  24. elapsedTime = 20;  
  25. StartActivity(typeof(SeeTime));  
  26. }  
  27.   
  28. void Counter()  
  29. {  
  30. elapsedTime--; // increment  
  31. timer.Text = elapsedTime.ToString();  
  32. myHandler.PostDelayed(r, delay_RATE);  
  33. if (elapsedTime == 0)  
  34. {  
  35. elapsedTime = 20;  
  36. }  
  37. }  
  38. void Run()  
  39. {  
  40. Counter();  
  41. }  
  42. }  
Code of SeeTimer Activity:
  1. public class SeeTime : Activity  
  2. {  
  3. MainActivity mainActivity;  
  4. private EditText timer;  
  5. protected override void OnCreate(Bundle savedInstanceState)  
  6. {  
  7. base.OnCreate(savedInstanceState);  
  8. SetContentView(Resource.Layout.Time);  
  9. timer = FindViewById(Resource.Id.txtTime);  
  10. mainActivity = new MainActivity();  
  11. timer.Text = mainActivity.timer.Text;  
  12. // Create your application here  
  13. }  
  14. }  

Answers (1)