I have a single activity with multiple views that represent different steps in a wizard. I have gotten code working that will save and restore the wizard to the correct screen, but after a save/restore cycle I cannot seem to get setContentView to work. The code executes without throwing any exceptions but the view isn't actually updated. Why would this be happening?
edit:
SOLVED I was using a handler to change screens, but on restore I wasn't using the newly constructed one, so messages were being sent to a handler that didn't have control of the screen.
You might be filling the contentview with data in onCreate rather than onResume. onCreate is not called again if you are doing a save/restore cycle. It might be worth checking which lifecycle method is called. It would also be helpful to know how you save/restore.
Related
From Activity lifecycle we know, that in onResume the UI is visible. But if I set a breakpoint on onResume, I still don't see it, only after that. I heard there's some method which can check it, but I can't remind how it calls. We used it to make better animation. So how can I be fully sured that UI is ready?
Add global layout listener on view you want to check. Also please make sure that you remove listener once your work is done else it will keep getting called multiple times.
#onik shared a good link that should solve your problem
This ensures lay-outing. Drawing happens continuously and frame are refreshed according to sys clock.
I recommend watching: https://www.youtube.com/watch?v=Q8m9sHdyXnE
This will give you good idea on android drawing and layouting
Good day,
I'm working on an application that will serve as a monitor of some sort for drivers. My client would like the application to work regardless of the orientation of the device.
I implemented the solution provided in the following article , and after fiddling a bit with the debugger, I can see that the Asynctask is still working. However, the TextViews and ImageViews it is supposed to work on are not working anymore.
Here is the code of my TaskFragment.
To clarify : The AsyncTask still receive and handle the elements correctly, but the elements of the layout are not updated anymore. I would like to know how I can keep them working.
I would suggest using an AsyncTaskLoader as those can re-attach to whatever lifecycle element you created it in relatively easily. See here: https://developer.android.com/reference/android/content/AsyncTaskLoader.html
It might seem pretty involved to implement at first, however if you read https://developer.android.com/guide/components/loaders.html, most of the weirdness should be cleared up.
TLDR: Using AsyncTaskLoader allows an easy way for your AsyncTask to be reattached to your fragment after it is destroyed and recreated. You just need to call getLoaderManager().initLoader(...) in the onCreate of your fragment.
Ok, so I found a (probably not very efficient) workaround, but a workaround nonetheless.
The problem was that, after an orientation change, since the Activity is destroyed and recreated, the variables batterymonitor, valuemonitor, etc, would not point towards the new objects created because of the layout/activity change.
As a solution, I am now using a findViewById each time I need to do an operation on the layout. This way, the id is permanently refreshed to keep up with the activity changes on a rotation of the device.
The ugly line I use to do so is :
batteryMonitor = (ImageView)getActivity().findViewById(R.id.batteryMonitor);
batteryMonitor.setImageResource(R.drawable.no_battery);
My MainActivity essentially shows two ImageViews. In order to make sure that the Bitmaps in the ImageViews are shown under all circumstances I do the following in the onResume method of MainActivity: load the Bitmaps bmp from preferences and assign them with
iView.setImageBitmap(bmp).
This works well after returning from another activity or when the user switches to another app and comes back to my app. But it does NOT work, when my app is on top of the screen, when the user switches off the device and turns it on again later. That is rather strange, since I explicitely load the Bitmaps from preferences and assign them again in onResume. This is also true for the latest devices e.g. the S7. Any advice highly appreciated!
you should check first if onResume() is called via
Log.d(..)
sorry i cant comment (50 rep)
Switching the device off and on would assume onStart and not onResume.
If you haven't done so do, whatever you did onResume onStart.
:)
It turned out that I had to do the assignment of the Bitmap via
iView.setImageBitmap(bmp)
already in the onCreate method. I assume that some layout inflation tasks are performed more thoroughly within onCreate than anywhere else. At least in the case of the turn off/on story this seems to be true.
I just want to ask why my app crashes with the following conditions.
I am working with fragments with only one activity. In my fragment, say FragmentA, I create views dynamically (inflating them). They work fine. But when I press home button, and go to the app again, I expect that FragmentA will be displayed but unfortunately, it throws NullPointer.
There is no errors in my android phone[GingerBread] but on my Tab, this error happens. Any help will be higly appreciated. Thanks.
You are not providing enough information to reliably answer this question, but the difference your're seeing is probably related to different behaviour of the garbage collector on the two different devices. As they are running on two different devices they will also behave differently.
The null-pointer you are seeing I would guess stems from wrongful use of member variables in your fragment.
When you go to the home screen and back your fragment will (or more accurately CAN) be recreated. Read: destroyed and re-instantiated using the argument-less constructor.
So make sure your fragment(s) are properly saving any needed state in onSaveInstanceState and restore this information in OnCreate/OnCreateView or whatever you're using.
I am changing the value of several RatingBars upon completion of a child activity (inside the onActivityResult() callback). My problem is that the parent activity has not finished drawing before my RatingBars value-changing code is executed, so I get some funky lag and a half-way completed "animation" before the parent layout has even been displayed.
I'm familiar with the document.ready() function in jQuery, which waits until the DOM is completely ready to commence any script therein.
Is there any way to achieve the same result with Android? In other words, I need a way to wait until an activity has completely finished drawing itself to the user's screen before some code is executed.
This might be a simple thing in Android, but I'm pretty noob. Thanks for your time and help.
-Steve
Could you simply put the code in the onResume method which will be called after the views have been set up?? (Not sure if this include getting drawn)
Another possibility is to create a handler and dispatch a method to it at the end of the onCreate method, this will get run on the UI thread but I imagine this won't get processed until the UI thread has finished the more important stuff (i.e. drawing the views)
This is largely just me putting down possible ideas, I know there is a way of achieving this I just can't remember how.