Widget Preferences "delay" problem - android

I have a Widget with his android.appwidget.action.APPWIDGET_CONFIGURE Activity with some preferences witch I can set before adding it to the "desktop".
My problem is that the preferences does not get applied because I noticed that as soon as the APPWIDGET_CONFIGURE is called, the widget is created and never refresh after the widget is actually applied to the screen and it actually never reads the updated preferences.
Any thoughts?
Thanks
Oh, I think I found what it seems to be an easy solution but I don't like it much so I like to have a "second opinion". I don't say what it is to not influence the answers.
PS. The configuration activity works fine, the problem is only when the widget is applied for the first time.

The (easy) solution I found is to call an update on the widget after closing the APPWIDGET_CONFIGURE Activity.
The "dirty" thing I don't like is that you actually "draw" the widget one extra time.

Related

Update a layout after an orientation change in a Fragment

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);

SeekBarPreferences are changing but not visually

I've been trying to work this out for 2 days now and I have absolutely no idea how to fix it.
Essentially, I have a service that listens for changes in a ListView (via a BroadcastReceiver) and updates several SeekBarPreferences accordingly via editor.putInt("SeekBarPreference", value).commit();. Now everything works as it should and it returns the correct values BUT it wont change the seekbarpreferences visually. If I restart the service (press back and then click on the relevant option) they change to the correct positions. Or even if I rotate the device they change.
I guess my question is; Is there any way to manually "refresh" the 'look' of the preferences?
Thanks!
(I know there are similar questions on here, but none of them really have a solution)
A lame way I sometimes fall back on is to relaunch the activity. Create an intent for the current activity and launch it. Two lines of code. Don't know if its a practical solution for your problem, but its worked in various situations for me.
did you try invalidate() or requestlayout() on that view?
Managed to get the seekbars updating on the ponSharedPreferenceChanged listener by clearing the preferences
PreferenceScreen prefScreen = getPreferenceScreen();
prefScreen.removeAll();
loading the xml again
addPreferencesFromResource(...etc.
Then re-sending the broadcast to update everything
It works, but it isn't very elegant. Thanks to everyone for helping!

Widget onUpdate() is not called when Android home screen rotates

I'm currently having issues with my widget. I'm using a Droid 2 phone and when I open the keyboard the screen rotates and my widget never calls the onUpdate() function call.
In efforts to debug the issue I overwrote the onReceive() and noticed that after the screen is rotated I get the intent action:
com.motorola.blur.home.ACTION_WIDGET_ADDED
After the rotation of the screen the TextViews loose their content so I need to be able to set text after the rotation. I usually set the text at the onUpdate() call.
Anyone have any ideas why this issue? I'm sure I'm doing something wrong...
Thanks,
-Jona
After a lot of testing and searching online I realized something so important about Widgets.
When the screen is rotated and the widget needs to be restored the Android system will restore it using the data from the last RemoteViews update.
I was updating parts of the widget separately so when it needed to restore only one part got updated.
The fix is always update everything on your RemoteViews all at once.

Using a Widget in Activity

it's me again :-)
Is there a way to use a widget (for home screen) in my own activites?
i think the homescreen is made with java, too?!
i do not want to change thinks in the widget, i still want to display them.
Regards
FLy
If you are looking to "re-use" a widget's layout and may be its base code, that is possible. I don't think it will be a good idea [or possible] to display a widget as an activity.
There are a lot of limitations in the number of view elements that can be used inside a widget. And again the way you will update a widget [RemoteViews] is actually very different from that of an Activity.
So in short, the answer would be a "NO".
Edit: This question has also been asked and answered a lot of times: difference between view and widget

Odd Orientation Change Behaviour in Android

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.

Categories

Resources