Android Question homescreen widget => orientation removes last updates

Marco Nissen

Active Member
Licensed User
Longtime User
Hi

I just implemented a homescreen widget, and it works when I actively update the remove view.
However, if I rotate the device, the images and labels default back to the initial settings of the view in the designer, i.e. the images are gone, and the labels only have template text

Is there any reason why this could happen, or to prevent this?
Could detect the orientation event maybe?

Thanks
Marco
 

Marco Nissen

Active Member
Licensed User
Longtime User
well, there is no problem to come up with the values, but the user elements seem to lose their state, and if I can't detect when the orientation change happens, I cannot do anything to change the state back to the original one.. so anybody knows how to detect it?
 
Upvote 0

dxxxyyyzzz

Member
Licensed User
Longtime User
Orientation change causes a Activity_Pause/Activity_Resume condition.
Save the user elements in Activity_Pause, restore them in Activity_Resume.
 
Upvote 0

Marco Nissen

Active Member
Licensed User
Longtime User
Orientation change causes a Activity_Pause/Activity_Resume condition.
Save the user elements in Activity_Pause, restore them in Activity_Resume.

Can i also use that for a widget/service which is not activity?

And: does anybody know how to implement the flipping mechanism for widgets or is that also not implemented?
That one that shows several stacked images and you slide back and forth ?

Thanks
Marco
 
Upvote 0

dxxxyyyzzz

Member
Licensed User
Longtime User
No. In the activity module, where you set up user elements, put the Save button and with button save the user settings.
B4X:
Sub Save_Click
    StateManager.SetSetting("name", "Marco")
    StateManager.SetSetting("surname", "Nissen")
    StateManager.SaveSettings
End Sub

In widget call settings under Widget_UpdateSettings

B4X:
Sub Widget_UpdateSettings
    name=StateManager.GetSetting("name")
    surname= StateManager.GetSetting("surname")
End Sub
 
Upvote 0

Marco Nissen

Active Member
Licensed User
Longtime User


Hi

unfortunately, this does not work.

The main activity is not in the foreground, so not interfering with the orientation change.

Currently, I am trying this:
  • display of the widget on the homescreen, showing the homescreen
  • click on update the widget to download current data, which works fine
  • change the orientation from portrait to landscape, some images disappear, all textlabels change back to the original setting of the designer layout
  • widget_updatesettings is never called
  • the remote view is called "RV", and rv_updatesettings is also never called
This is what I wrote (without the unimportant details like data download etc)

B4X:
Sub Process_Globals
    Dim rv As RemoteViews
End Sub

Sub Service_Create
    rv = ConfigureHomeWidget("HotRightNowView", "rv", 5, "HotRightNow")
End Sub

Sub Service_Start (StartingIntent As Intent)
    If rv.HandleWidgetEvents(StartingIntent) Then Return
End Sub

Sub rv_RequestUpdate
      ToastMessageShow("widget: request update", False)
End Sub

Sub rv_Disabled
    StopService("")
End Sub

Sub Service_Destroy
End Sub

Sub performSearch_click
      ToastMessageShow("widget: perform search", False)
    ' download operations, works fine
End Sub

Sub JobDone (Job As HttpJob)
    ' make modifications after download
End Sub

Sub service_UpdateSettings
    ToastMessageShow("service: update settings", False)
    ' never called
End Sub

Sub rv_UpdateSettings
    ToastMessageShow("rv: update settings", False)
    ' never called
End Sub

Sub Widget_UpdateSettings
    ToastMessageShow("widget: update settings", False)
    ' never called
End Sub
 
Last edited:
Upvote 0

Marco Nissen

Active Member
Licensed User
Longtime User
That does not work.
Again, it is not about remembering and restoring the state.

The orientation does not fire any event whatsoever.
It invalidates the user interface elements, but does not do anything else

Can somebody please help . THanks
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I personally use a timer in services to track rotation. I store the orientation and every second or 2 and I also check if the orientation has changed among other things. If the orientation has changed I reload the data and images and everything start showing up in the widget again. As I store all previously used(last used) images in assets and all text is stored in a JSON file, it's extremely simply to get the data back as it was before the screen had rotated.

I do know exactly where you are coming from @Marco Nissen. I had the exact same issue with a weather widget that I developed and the images disappearing on screen rotation. Storing the last orientation and using a timer to check and reload the imaged is necessary fixed my problem 100%.

Here, check out the following link http://www.b4x.com/android/forum/threads/catching-screen-rotation-in-services-with-a-timer.39453/
 
Upvote 0

Marco Nissen

Active Member
Licensed User
Longtime User
Hey Peter, that is a great idea. I'll do that, that'll work. However, it would be *much* better to have a triggered event for that .. Thanks for the hint, Marco
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
When I dynamically load images into widgets this always happens to me when the device rotation changes. There are other post on this community which I've read in the past where other developers have had the exact same issue. That's why I came up with the idea of storing the current rotation and using a timer.

Hello @Erel, I've developed a few widgets that rely on dynamically loading images into ImageViews at runtime, it doesn't matter what I do it always does the same thing. If I load an image into ImageViews in the designer then run the widget and rotate the screen, it just works perfect. I've only ever come across this issue when dynamically loading and changing images. Well it's not exactly an issue, rotating clears my dynamically loaded ImageViews thus images need reloading again.

Anyway I'm not too bothered as the timer works for me perfectly fine.

Cheers...
 
Last edited:
Upvote 0

Bryan

Member
Licensed User
Longtime User
I am experiencing the same problems with using an imageview as part of a widget. I implemented a timer to check screen orientation and then reload the imageview contents. This works but I am wondering about the impact on CPU cycles or CPU load running a timer that triggers every 500 milliseconds. Or how it might affect using "StartServiceAt" which I use for an elapse time counter over an extended period of time.

Also, there is an issue with opening other applications over the top of the Widget on your home screen. Android device settings screen will clear the contents of my imageview if opened while on my home screen. Is there some way to fix that issue. Other applications do the same thing if opened over the top of a widget's imageview such as EMail if used on your home screen. I searched this forum and have not seen this being addressed by anyone as far as I can tell.
 
Upvote 0

Bryan

Member
Licensed User
Longtime User
No, actually I'm not using it to display Images only text. As in the code posted below. This is reduced copy of my code so not every thing is shown
B4X:
Sub Process_Globals
Dim CatList As List
Dim SelectedCatStr As String
Dim kvs As KeyValueStore
End Sub

    If File.Exists(File.DirRootExternal & "/TcLogger", "Catagories.txt") = True Then
    CatList = File.ReadList(File.DirRootExternal & "/TcLogger", "Catagories.txt")
    If kvs.ContainsKey("SelectedCat") Then
    SelectedCatStr = kvs.GetSimple("SelectedCat")
    rv.settext("CatLbl",SelectedCatStr)
    rv.UpdateWidget

Similar code is used with a timer to refresh the imageview text during a screen orientation change. Timer runs every 500 milliseconds. I do load a png image into the imageview in designer. Text is displayed on top of the loaded Image. Imageview always retains the the .png image during screen rotation and if obscured by another app. It's only the text that gets cleared out.

Bryan
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…