I have created a TileService for watch app. When i switch to my TileService every time onCreate method is called multiple times (3 or 4 times).
As TileService is a Service why onCreate is called multiple times?
And when I switch to my TileService by swiping it doesn't call onTileRequest for every onCreate to update UI.
From official doc onTileRequest called every time user have a action on UI.
Can anyone explain me the lifecycle of TileService .
How can i update Tile UI every time onCreate is called ?
I need latest UI when user switch to the Tile by swiping.
Try using onTileEnterEvent. It's called every time the tile is visible.
A simple example that causes the Tile to render a new UI.
override fun onTileEnterEvent(requestParams: EventBuilders.TileEnterEvent){
TileService.getUpdater(this)
.requestUpdate(MyTileService::class.java)
}
Related
The Android TileService documentation states the following:
When a tile should be up to date and listing will be indicated by onStartListening() and onStopListening().
Based on the lifecycle for a TileService, it's intended that you START updating your tiles state when onStartListening() is called.
The issue is that it doesn't seem that this callback is called immediately when the tile enters the users view. Meaning it can show outdated information for several seconds some times before the view is properly updated.
An example of this would be that a service is Connected, the tile shows it as connected. I then switch to the application, turn off the service, then open the Quick Settings view back up. It still shows the service as connected for a long time.
In theory, the moment that the tile came into view it should have updated with the new data and displayed it as not connected.
I've confirmed this is the case by simply adding a log message as the first entry in the view. When I bring the Tile into the users view, that log message isn't written to log cat for upwards of 4-5 seconds.
So, what's going on? Am I missing something?
My Code
#Override
public void onStartListening() {
android.util.Log.d("Test", "onStartListening called");
updateTileDisplay();
registerDelegate();
}
The updateTileDisplay(); function is updating the title, state and icon for the Tile. Once it completes it is calling tile.updateTile(); as it should be. Additionally, the registerDelegate(); function is registering a delegate that will listen for internal state changes and update the tile accordingly. This delegate is later removed when onStopListening() is called.
I do not see that onStartListening called log message until upwards of 5 seconds after I have brought my tile into view. This seems not to line up with the TileService documentation.
I'm using rxjava and rxandroid. In my presenter i have used
Observable.timer(10, TimeUnit.SECONDS);
And I would like to save time on screen orientation change. For example timer starts and after 3 seconds user is changing screen orientation, and timer starts again but from 0 for 10 seconds, but it needs to run for 7 remaining seconds.
Do you know any solution?
When you orientate your device, it recall onCreate() method of your activity and redraw your views. For this you can create a helper fragment and put your operations in that fragment. Put this line setRetainInstance(true); in onCreate() method. Then, do your operations onAsyncTask. If you wand display each second of timer in your screen, then call onProgressUpdate() method of your AsyncTask class. Then, create an interface, withProgress callback and put this callback in onProgressUpdate() method. Then you can handle this operations via implementing that interface which you created in your fragment. I have simple repository in github which can help you. Enjoy. This article also can help you: Enjoy.
Hi I have gone though activity lifecyle on many threads, but I could not find what we should do in onStart, onResume, onPause method of the activity.
In the onStart() method, you add code that's relevant at the beginning of the activity.
Let's say, you have an app that reads the temperature of the device's battery. You'll want to have an initial value, so as to show the user.
So in the onStart(), you'd add code that goes ahead and fetches the information you'd need, and displays it for the user, before your timer (for example) goes and reads the information a minute later.
The onPause() method is called before the application goes in to the background.
To stay with our example, in the onPause() method, you'd save the last recorded temperature to the device; so you can show a comparison when the user next opens the app.
The onResume() method is called when the application is brought back to the foreground (i.e.: you've gone to the task manager, and tapped on your app to show it again).
Again, staying with the going example; in the onResume() method, you'd go ahead, read your saved data, load fresh data, and show a comparison of the two in the application.
Then, when your timer ticks next, only fresh data will be shown.
Your question is a bit vague, so answer might not be super specific..
I would say there are no strict "rules" around what we should do in corresponding activity lifecycle methods.
In fact, you can do nothing there (just make sure you call super method if you decided to override those). I.e. your custom activity might not even override these methods - it will work just fine.
onStart, onResume and onPause methods are just hints to you about activity lifecycle change, so you can react accordingly, i.e. start/stop specific to your activity operations at the appropriate time.
For instance, when onResume is called it means that activity became fully visible to the user, so you might want to start some animation (if necessary)
Again, you are not obligated to put any code in there.
Usually most of the operations are performed within oncreate and onresume.
However for your info let me brief it out,
Onstart- this is called after Oncreate, once activity is visible to the user, if you want to perform some operations before the visibility do it in Oncreate, because most of codes should be operated before user views the activity.
OnResume-Be cautious on Onresume is it is quite tricky it will be called whenever you activity is brought to foreground.
Onpause-Called before Onresume, codes wont be executed here, so strictly avoid adding codes in Onpause instead add inside Onresume.
Hope it helps,
My android application has 2 activities:
In the first one (MainActivity), the user chooses some parameters and these parameters are sent to the second activity (Display).
The second activity calls a web service, and according to the chosen parameters, the web service returns a value. I use the returned value to draw a bar chart of the evolution of this value. That's why I created a timer in the second activity that I put in the onCreate() function:
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
public void run() {
finish();
startActivity(getIntent());
}
},10000, 10000);
So every 10 seconds, the second activity is called again and the bar chart is updated with the new returned value.
The problem is, after the 2nd or the 3rd execution of the timer, several identical values are returned at the same time, as if the activity was called several times. And then the application starts freezing (but doesn't close).
I'm using the charts provided by this library: http://android-graphview.org/
I've also tried using the functions provided on the above website (resetData and appendData), and also the invalidate() function, but nothing works.
Any ideas why this happens? Is my way of refreshing the activity wrong?
The way you described it:
you create a new Timer (see 2.) with every call of the second activity
The purpose of each Timer is to call the second activity (see 1.) every 10 seconds
As a result the amount of times the second activity is called as a function of time increases exponentially.
A possible solution would be to move the timer to the onCreate method of your Main Activity (and still call the second activity from it).
This way there should be exactly one Timer active at any time.
EDIT:
As commented by Marius, an Activity might not be the optimal choice. If there is no user-input and the only thing the activity does is call a webservice and return the result, a method called from your Main Activity would be sufficient.
Firstly, calling finish() and starting the activity, NOT A GOOD IDEA.
Secondly, As far as i have understood your scenario, calling an AsyncTask inside a Timer after every 10 seconds is a better way to accomplish this. Call your web service in doInBackground() and then update your UI from onPostExecute(), this way you can avoid calling finish() and relaunching your activity every 10 seconds.
Finally you are creating a new Timer instance eveytime your Activity is called, so creating a huge number of Timer instances hanging your application.
I am calculating creating apps for Online Shopping in which i am having a Deals Start Date and End Date..
From this two date i want to calculate the time remaining for the Deal to end in Seconds basis.. I calculated remaining days,seconds,hours,minutes..
I want to display the Remaining time in Days:Hours:Minutes:Seconds format...
Although i move to next activity this time should runs in background in which the second must updated every seconds..
If i move this activity again i want to display the Updated Remaining time...
Also in my UI the Remaining time must get reduced second by second.
Instead of building a service I would create a Handler that updates your Userinterface.
You can now call sendEmptyMessageDelayed with a delay of 1000ms. In the handleMessage method you can update your UI and call sendEmptyMessageDelayed again.
Don't count on Android to call you exactly in time. recalculate the remaining time every now and then instead of just decreasing it by one.
If the activity is in the background you shouldn't update the UI because the activity is paused. Just disable the whole updating process in the onPause method and reenable it in your onResume method. If you method is destroyed from the system while it was in the background your onCreate method will be called again and you have to recalculate the actual remaining time.
Make sure you understand the ActivityLifecycle Process before implementing this changes.
Sounds like you need a Service and an Activity with the Service performing the timing operation and the Activity presenting the time information.