how to update UI on a tabbed activity which uses fragments and timersHow to save an Android Activity state using save instance state?How do I update the GUI from another thread?How do I pass data between Activities in Android application?How do I create a transparent Activity on Android?Separate Back Stack for each tab in Android using FragmentsWhy fragments, and when to use fragments instead of activities?Dilemma: when to use Fragments vs Activities:A TextView text scroll turn to the start position when a timer execute RunOnUiThread on Xamarin.AndroidHow to use a chronometer and properly update variables in a fragment?Fragment backstack with tab fragment

Doubt about a particular point of view on how to do character creation

Heuristic argument for the Riemann Hypothesis

How to investigate an unknown 1.5GB file named "sudo" in my Linux home directory?

Received email from ISP saying one of my devices has malware

I failed to respond to a potential advisor

Don't look at what I did there

Necessity of tenure for lifetime academic research

Does the telecom provider need physical access to the SIM card to clone it?

Why does the U.S. military maintain their own weather satellites?

Heavy Box Stacking

Who declared the Last Alliance to be the "last" and why?

How did the Altair 8800 front panel load the program counter?

What is the practical impact of using System.Random which is not cryptographically random?

Is the word 'mistake' a concrete or abstract noun?

What is this "opened" cube called?

Why haven't the British protested Brexit as ardently as the Hong Kong protesters?

Does the Freedom of Movement spell prevent petrification by the Flesh to Stone spell?

Can the inductive kick be discharged without a freewheeling diode, in this example?

Is this statement about a motion being simple harmonic in nature strong?

'Horseshoes' for Deer?

Can two aircraft be allowed to stay on the same runway at the same time?

Ideas behind the 8.Bd3 line in the 4.Ng5 Two Knights Defense

Why do motor drives have multiple bus capacitors of small value capacitance instead of a single bus capacitor of large value?

How can I store milk for long periods of time?



how to update UI on a tabbed activity which uses fragments and timers


How to save an Android Activity state using save instance state?How do I update the GUI from another thread?How do I pass data between Activities in Android application?How do I create a transparent Activity on Android?Separate Back Stack for each tab in Android using FragmentsWhy fragments, and when to use fragments instead of activities?Dilemma: when to use Fragments vs Activities:A TextView text scroll turn to the start position when a timer execute RunOnUiThread on Xamarin.AndroidHow to use a chronometer and properly update variables in a fragment?Fragment backstack with tab fragment






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a tabbed activity with 3 fragments. First fragment allows user to select a test, Third fragment has a timer and a text view and some buttons. Second fragment has nothing in it as of now.



Inside fragment 3 I have a button that starts a timer, once the timer is started the textview refreshes every minute showing the time elapsed. Everything works well till this point.



Problem: Once the timer is started if I select fragment 1 and return back to fragment 3, the timer stops updating the textView. I know that the timer_elapsed is running correctly only textview is not updating.



I have tried FragmentActivity.RunOnUiThread(() => ); this works fine till the fragments are switched.



I did try using Loopers.MainLooper to update the screen and the problem still remained the same.



// update the screen every minute
if (mActivity != null)

mActivity.RunOnUiThread(() =>


// set the progress bar
progressBar.Progress = i32ProgressBarValue;
textViewPercentage.Text = i32ProgressBarValue + "%";

// set the text view
textViewTestTime.Text = $"Globals.i32Days" + "D :" + $"Globals.i32Hours" + "H :" + $"Globals.i32Mins" + "M";
);



I would like the textView to continue updating correctly when the fragments are switched and returned back to fragment 3










share|improve this question






























    0















    I have a tabbed activity with 3 fragments. First fragment allows user to select a test, Third fragment has a timer and a text view and some buttons. Second fragment has nothing in it as of now.



    Inside fragment 3 I have a button that starts a timer, once the timer is started the textview refreshes every minute showing the time elapsed. Everything works well till this point.



    Problem: Once the timer is started if I select fragment 1 and return back to fragment 3, the timer stops updating the textView. I know that the timer_elapsed is running correctly only textview is not updating.



    I have tried FragmentActivity.RunOnUiThread(() => ); this works fine till the fragments are switched.



    I did try using Loopers.MainLooper to update the screen and the problem still remained the same.



    // update the screen every minute
    if (mActivity != null)

    mActivity.RunOnUiThread(() =>


    // set the progress bar
    progressBar.Progress = i32ProgressBarValue;
    textViewPercentage.Text = i32ProgressBarValue + "%";

    // set the text view
    textViewTestTime.Text = $"Globals.i32Days" + "D :" + $"Globals.i32Hours" + "H :" + $"Globals.i32Mins" + "M";
    );



    I would like the textView to continue updating correctly when the fragments are switched and returned back to fragment 3










    share|improve this question


























      0












      0








      0








      I have a tabbed activity with 3 fragments. First fragment allows user to select a test, Third fragment has a timer and a text view and some buttons. Second fragment has nothing in it as of now.



      Inside fragment 3 I have a button that starts a timer, once the timer is started the textview refreshes every minute showing the time elapsed. Everything works well till this point.



      Problem: Once the timer is started if I select fragment 1 and return back to fragment 3, the timer stops updating the textView. I know that the timer_elapsed is running correctly only textview is not updating.



      I have tried FragmentActivity.RunOnUiThread(() => ); this works fine till the fragments are switched.



      I did try using Loopers.MainLooper to update the screen and the problem still remained the same.



      // update the screen every minute
      if (mActivity != null)

      mActivity.RunOnUiThread(() =>


      // set the progress bar
      progressBar.Progress = i32ProgressBarValue;
      textViewPercentage.Text = i32ProgressBarValue + "%";

      // set the text view
      textViewTestTime.Text = $"Globals.i32Days" + "D :" + $"Globals.i32Hours" + "H :" + $"Globals.i32Mins" + "M";
      );



      I would like the textView to continue updating correctly when the fragments are switched and returned back to fragment 3










      share|improve this question














      I have a tabbed activity with 3 fragments. First fragment allows user to select a test, Third fragment has a timer and a text view and some buttons. Second fragment has nothing in it as of now.



      Inside fragment 3 I have a button that starts a timer, once the timer is started the textview refreshes every minute showing the time elapsed. Everything works well till this point.



      Problem: Once the timer is started if I select fragment 1 and return back to fragment 3, the timer stops updating the textView. I know that the timer_elapsed is running correctly only textview is not updating.



      I have tried FragmentActivity.RunOnUiThread(() => ); this works fine till the fragments are switched.



      I did try using Loopers.MainLooper to update the screen and the problem still remained the same.



      // update the screen every minute
      if (mActivity != null)

      mActivity.RunOnUiThread(() =>


      // set the progress bar
      progressBar.Progress = i32ProgressBarValue;
      textViewPercentage.Text = i32ProgressBarValue + "%";

      // set the text view
      textViewTestTime.Text = $"Globals.i32Days" + "D :" + $"Globals.i32Hours" + "H :" + $"Globals.i32Mins" + "M";
      );



      I would like the textView to continue updating correctly when the fragments are switched and returned back to fragment 3







      c# android xamarin.android system.timers.timer






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 23:32









      Swathi AyasSwathi Ayas

      32 bronze badges




      32 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          1















          i wirte a sample use Timer in a TabbedActivity(use BottomNavigationView) which alse has three Fragments,in the third fragment,i use button to start the Timer for update button's text every two seconds, it works well (also works when switch fragments),below is the code in third fragment,you could refer to it:



          Timer _dispatcherTimer;
          TimerCallback timerDelegate;

          public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

          // Use this to return your custom view for this Fragment
          // return inflater.Inflate(Resource.Layout.YourFragment, container, false);
          View view = inflater.Inflate(Resource.Layout.fragment_account, container, false);
          init(view);
          timerDelegate = new TimerCallback(Tick);

          return view;

          private void init(View view)

          button = view.FindViewById<Button>(Resource.Id.mybutton);
          button.Click += delegate

          _dispatcherTimer = new System.Threading.Timer(timerDelegate, null, 0, 2000);
          ;

          private void Tick(object state)


          this.Activity.RunOnUiThread(() =>

          //do something
          Random reRandom = new Random();
          int s = reRandom.Next(1000);
          button.Text = s.ToString();
          );






          share|improve this answer

























          • Thank you for the solution! The callback timer works well. I had to make a small tweak to the code to get it working. The _dispatchedTimer turns out to be null everytime we return to fragment 3, so added the following lines before return view in the OnCreateView(..) get it working: _dispatchedTimer.Dispose(); and dispatcherTimer = new System.Threading.Timer(Globals.timerDelegate, null, 0, 2000);

            – Swathi Ayas
            Mar 28 at 21:01












          • This way the timer interval would not get incremented and the screen started updating. Thank you for the help !

            – Swathi Ayas
            Mar 28 at 21:04











          • Since we are switching fragments, added this check as well if (this.Activity != null) before updating the UI

            – Swathi Ayas
            Mar 28 at 21:48











          • @SwathiAyas yes,my sample is suitable for my needs, which should be adjusted according to your own project. If it is helpful to you, could you mark it ? Thanks !

            – Leo Zhu - MSFT
            Mar 29 at 0:46










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388063%2fhow-to-update-ui-on-a-tabbed-activity-which-uses-fragments-and-timers%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1















          i wirte a sample use Timer in a TabbedActivity(use BottomNavigationView) which alse has three Fragments,in the third fragment,i use button to start the Timer for update button's text every two seconds, it works well (also works when switch fragments),below is the code in third fragment,you could refer to it:



          Timer _dispatcherTimer;
          TimerCallback timerDelegate;

          public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

          // Use this to return your custom view for this Fragment
          // return inflater.Inflate(Resource.Layout.YourFragment, container, false);
          View view = inflater.Inflate(Resource.Layout.fragment_account, container, false);
          init(view);
          timerDelegate = new TimerCallback(Tick);

          return view;

          private void init(View view)

          button = view.FindViewById<Button>(Resource.Id.mybutton);
          button.Click += delegate

          _dispatcherTimer = new System.Threading.Timer(timerDelegate, null, 0, 2000);
          ;

          private void Tick(object state)


          this.Activity.RunOnUiThread(() =>

          //do something
          Random reRandom = new Random();
          int s = reRandom.Next(1000);
          button.Text = s.ToString();
          );






          share|improve this answer

























          • Thank you for the solution! The callback timer works well. I had to make a small tweak to the code to get it working. The _dispatchedTimer turns out to be null everytime we return to fragment 3, so added the following lines before return view in the OnCreateView(..) get it working: _dispatchedTimer.Dispose(); and dispatcherTimer = new System.Threading.Timer(Globals.timerDelegate, null, 0, 2000);

            – Swathi Ayas
            Mar 28 at 21:01












          • This way the timer interval would not get incremented and the screen started updating. Thank you for the help !

            – Swathi Ayas
            Mar 28 at 21:04











          • Since we are switching fragments, added this check as well if (this.Activity != null) before updating the UI

            – Swathi Ayas
            Mar 28 at 21:48











          • @SwathiAyas yes,my sample is suitable for my needs, which should be adjusted according to your own project. If it is helpful to you, could you mark it ? Thanks !

            – Leo Zhu - MSFT
            Mar 29 at 0:46















          1















          i wirte a sample use Timer in a TabbedActivity(use BottomNavigationView) which alse has three Fragments,in the third fragment,i use button to start the Timer for update button's text every two seconds, it works well (also works when switch fragments),below is the code in third fragment,you could refer to it:



          Timer _dispatcherTimer;
          TimerCallback timerDelegate;

          public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

          // Use this to return your custom view for this Fragment
          // return inflater.Inflate(Resource.Layout.YourFragment, container, false);
          View view = inflater.Inflate(Resource.Layout.fragment_account, container, false);
          init(view);
          timerDelegate = new TimerCallback(Tick);

          return view;

          private void init(View view)

          button = view.FindViewById<Button>(Resource.Id.mybutton);
          button.Click += delegate

          _dispatcherTimer = new System.Threading.Timer(timerDelegate, null, 0, 2000);
          ;

          private void Tick(object state)


          this.Activity.RunOnUiThread(() =>

          //do something
          Random reRandom = new Random();
          int s = reRandom.Next(1000);
          button.Text = s.ToString();
          );






          share|improve this answer

























          • Thank you for the solution! The callback timer works well. I had to make a small tweak to the code to get it working. The _dispatchedTimer turns out to be null everytime we return to fragment 3, so added the following lines before return view in the OnCreateView(..) get it working: _dispatchedTimer.Dispose(); and dispatcherTimer = new System.Threading.Timer(Globals.timerDelegate, null, 0, 2000);

            – Swathi Ayas
            Mar 28 at 21:01












          • This way the timer interval would not get incremented and the screen started updating. Thank you for the help !

            – Swathi Ayas
            Mar 28 at 21:04











          • Since we are switching fragments, added this check as well if (this.Activity != null) before updating the UI

            – Swathi Ayas
            Mar 28 at 21:48











          • @SwathiAyas yes,my sample is suitable for my needs, which should be adjusted according to your own project. If it is helpful to you, could you mark it ? Thanks !

            – Leo Zhu - MSFT
            Mar 29 at 0:46













          1














          1










          1









          i wirte a sample use Timer in a TabbedActivity(use BottomNavigationView) which alse has three Fragments,in the third fragment,i use button to start the Timer for update button's text every two seconds, it works well (also works when switch fragments),below is the code in third fragment,you could refer to it:



          Timer _dispatcherTimer;
          TimerCallback timerDelegate;

          public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

          // Use this to return your custom view for this Fragment
          // return inflater.Inflate(Resource.Layout.YourFragment, container, false);
          View view = inflater.Inflate(Resource.Layout.fragment_account, container, false);
          init(view);
          timerDelegate = new TimerCallback(Tick);

          return view;

          private void init(View view)

          button = view.FindViewById<Button>(Resource.Id.mybutton);
          button.Click += delegate

          _dispatcherTimer = new System.Threading.Timer(timerDelegate, null, 0, 2000);
          ;

          private void Tick(object state)


          this.Activity.RunOnUiThread(() =>

          //do something
          Random reRandom = new Random();
          int s = reRandom.Next(1000);
          button.Text = s.ToString();
          );






          share|improve this answer













          i wirte a sample use Timer in a TabbedActivity(use BottomNavigationView) which alse has three Fragments,in the third fragment,i use button to start the Timer for update button's text every two seconds, it works well (also works when switch fragments),below is the code in third fragment,you could refer to it:



          Timer _dispatcherTimer;
          TimerCallback timerDelegate;

          public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

          // Use this to return your custom view for this Fragment
          // return inflater.Inflate(Resource.Layout.YourFragment, container, false);
          View view = inflater.Inflate(Resource.Layout.fragment_account, container, false);
          init(view);
          timerDelegate = new TimerCallback(Tick);

          return view;

          private void init(View view)

          button = view.FindViewById<Button>(Resource.Id.mybutton);
          button.Click += delegate

          _dispatcherTimer = new System.Threading.Timer(timerDelegate, null, 0, 2000);
          ;

          private void Tick(object state)


          this.Activity.RunOnUiThread(() =>

          //do something
          Random reRandom = new Random();
          int s = reRandom.Next(1000);
          button.Text = s.ToString();
          );







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 28 at 2:20









          Leo Zhu - MSFTLeo Zhu - MSFT

          3,5551 gold badge1 silver badge8 bronze badges




          3,5551 gold badge1 silver badge8 bronze badges















          • Thank you for the solution! The callback timer works well. I had to make a small tweak to the code to get it working. The _dispatchedTimer turns out to be null everytime we return to fragment 3, so added the following lines before return view in the OnCreateView(..) get it working: _dispatchedTimer.Dispose(); and dispatcherTimer = new System.Threading.Timer(Globals.timerDelegate, null, 0, 2000);

            – Swathi Ayas
            Mar 28 at 21:01












          • This way the timer interval would not get incremented and the screen started updating. Thank you for the help !

            – Swathi Ayas
            Mar 28 at 21:04











          • Since we are switching fragments, added this check as well if (this.Activity != null) before updating the UI

            – Swathi Ayas
            Mar 28 at 21:48











          • @SwathiAyas yes,my sample is suitable for my needs, which should be adjusted according to your own project. If it is helpful to you, could you mark it ? Thanks !

            – Leo Zhu - MSFT
            Mar 29 at 0:46

















          • Thank you for the solution! The callback timer works well. I had to make a small tweak to the code to get it working. The _dispatchedTimer turns out to be null everytime we return to fragment 3, so added the following lines before return view in the OnCreateView(..) get it working: _dispatchedTimer.Dispose(); and dispatcherTimer = new System.Threading.Timer(Globals.timerDelegate, null, 0, 2000);

            – Swathi Ayas
            Mar 28 at 21:01












          • This way the timer interval would not get incremented and the screen started updating. Thank you for the help !

            – Swathi Ayas
            Mar 28 at 21:04











          • Since we are switching fragments, added this check as well if (this.Activity != null) before updating the UI

            – Swathi Ayas
            Mar 28 at 21:48











          • @SwathiAyas yes,my sample is suitable for my needs, which should be adjusted according to your own project. If it is helpful to you, could you mark it ? Thanks !

            – Leo Zhu - MSFT
            Mar 29 at 0:46
















          Thank you for the solution! The callback timer works well. I had to make a small tweak to the code to get it working. The _dispatchedTimer turns out to be null everytime we return to fragment 3, so added the following lines before return view in the OnCreateView(..) get it working: _dispatchedTimer.Dispose(); and dispatcherTimer = new System.Threading.Timer(Globals.timerDelegate, null, 0, 2000);

          – Swathi Ayas
          Mar 28 at 21:01






          Thank you for the solution! The callback timer works well. I had to make a small tweak to the code to get it working. The _dispatchedTimer turns out to be null everytime we return to fragment 3, so added the following lines before return view in the OnCreateView(..) get it working: _dispatchedTimer.Dispose(); and dispatcherTimer = new System.Threading.Timer(Globals.timerDelegate, null, 0, 2000);

          – Swathi Ayas
          Mar 28 at 21:01














          This way the timer interval would not get incremented and the screen started updating. Thank you for the help !

          – Swathi Ayas
          Mar 28 at 21:04





          This way the timer interval would not get incremented and the screen started updating. Thank you for the help !

          – Swathi Ayas
          Mar 28 at 21:04













          Since we are switching fragments, added this check as well if (this.Activity != null) before updating the UI

          – Swathi Ayas
          Mar 28 at 21:48





          Since we are switching fragments, added this check as well if (this.Activity != null) before updating the UI

          – Swathi Ayas
          Mar 28 at 21:48













          @SwathiAyas yes,my sample is suitable for my needs, which should be adjusted according to your own project. If it is helpful to you, could you mark it ? Thanks !

          – Leo Zhu - MSFT
          Mar 29 at 0:46





          @SwathiAyas yes,my sample is suitable for my needs, which should be adjusted according to your own project. If it is helpful to you, could you mark it ? Thanks !

          – Leo Zhu - MSFT
          Mar 29 at 0:46








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388063%2fhow-to-update-ui-on-a-tabbed-activity-which-uses-fragments-and-timers%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

          밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

          1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴