• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Update a Textview in a "still not created" Activity.

Funesto

Lurker
Hello. I Know the title of this post is quite twisted, so I will try to explain myself in my poor english! I am new at programming everything (less than one month experience), so my APP is far to be perfect or built in the right way!

My App opens up with eight buttons: seven of them leads to timers and one that leads to a page where all timers (counters) shows together. (In first thumb image you can see the BLUE button to the value of all timers together and the RED buttons to each of the seven timers).

To achieve this, I created seven "Services" classes and seven "public static textviews" (Oh yes, I'm guilty) updated by each service when started.

Now the problems comes with the "General Page" in which I want to show the started times in "Real Time". In the beginning I thought I could update this page with .... other public static textviews, directly from service files. But ... of course If I start a timer it crashes, because that
"general textview" is not yet created! To make it work I have to open (tap) general button first ... in this case timers won't crash because service files "know" what to update.

Using strings won't work, as I am getting a "fixed" value of the timer at the moment the general page is called.... and using a WHILE cycle to keep that string called (and showed) is freezing the App.

I also thought to use a boolean to know if general page was already called. If "false" .... on tapping to one of the counter, the APP would open the "general page" and then quickly back to the chosen counter ... I was not able to achieve IT.

What would you do (as a noob) in a case like this? (beside giving up of course!!!!)


Thank you very much for your help! :)
 

Attachments

  • Main Menu (Main Activity).png
    Main Menu (Main Activity).png
    92.7 KB · Views: 152
  • Timer Page .png
    Timer Page .png
    20.4 KB · Views: 107
  • General Page.png
    General Page.png
    32.1 KB · Views: 71
Hi, and welcome to Android Forums.
From my understanding of your description, it sounds like you want to start several timers, which can update your 'General' page in realtime. Normally the way to go about this is to use multi-threading. As you've observed, having a fixed loop in your main activity thread is a sure way of freezing your app.

There's a class in the Android framework called AsyncTask. It's tailor made for what you're trying to do. AsyncTask will run a background task in a separate thread, and allow you to update UI components on the main thread.
Btw the "main" thread is the one which processes UI events, and is associated with your MainActivity class.

So to begin with, have a look at AsyncTask, and decide if this can help you achieve your aims. I tend to think it will be simpler than the solution you currently have, involving services. The link below is to the Android dev website, but there are many other code examples showing how to use AsyncTask.

https://developer.android.com/reference/android/os/AsyncTask.html
 
Back
Top Bottom