Android Question App activity vs widget activity

Paulo Rosa

Member
Licensed User
Hi,

My app has a main activity and also a widget. Currently, I'm using two activities, one is for the app's main "screen" and the other is for the widget. This scheme is hard to work with, since B4A is not able to debug into widgets, and I need to have the same appearance and the same logic on both activities. So, I am simply replicating the views and logic from one activity into the other activity. Hard to develop and maintain...

Maybe it's a dumb question but... can I use only one activity and share this activity and it's views between the main "screen" and the widget? How to manage events, variables and subs in that case?

Regards,
 

Peter Simpson

Expert
Licensed User
Longtime User
I take it that the main activity is some sort of setting screen for the widget?

Well the widget is a service, so you could just save the main activity settings to a file and just let the widget read the settings from the file, thus you do not need two activities.

I presume that is what you are trying to do @Paulo Rosa, if not then please explain in more detail...
 
Upvote 0

Paulo Rosa

Member
Licensed User
I take it that the main activity is some sort of setting screen for the widget?

Well the widget is a service, so you could just save the main activity settings to a file and just let the widget read the settings from the file, thus you do not need two activities.

I presume that is what you are trying to do @Paulo Rosa, if not then please explain in more detail...

Hi, Peter.

Currently my app shows info about the Wi-Fi access point the phone is connected to. Both the main activity and the widget shows the same information: the SSID and the signal level of the AP. If I want to connect to another AP, I press on the image that shows the signal level, which opens the standard Android Wi-Fi settings screen

As a matter of fact, I don't need the main activity, for the goal of the app is only a widget. But, as I said before, B4A doesn't allow debugging widgets. That's why I'm using two activities, simply replicating the main activity logic and views into the widget activity. I do the debugging in the main activity and if necessary I correct/change it's code. After I'm satisfied with the result, do the same code change on the widget activity. It would be much more simpler if I could use only one activity.
 
Last edited:
Upvote 0

Paulo Rosa

Member
Licensed User
I'm not using timers, because that would impact the battery usage. I'm using Broadcast Receivers to capture events like Wi-Fi on/off and Wi-Fi signal level changes.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
You can use 'StartServiceAt' or StartServiceAtExact which is what I personally use and restart your service every minute or so (I find it does start when it's supposed to (within 1 second)). You battery should not really take a battering, none of my widgets do and some are doing a let of work.

Don't forget to add 'Service.StopAutomaticForeground', better still read Erels tutorial here https://www.b4x.com/android/forum/threads/automatic-foreground-mode.90546/#content.

Enjoy...
 
Upvote 0
Top