Android Question Resumable Subs with Classes

Creideiki

Member
Licensed User
Hi there,

I have quite a difficult problem here. I attatched a little project, which shows what happens.

I have a data class (called DataContainer here), where the instances are distributed between different activities. Some of the methods are using UI features. These features need the activity to be active, which instantiated the class... so this failes when the class instance is used by another activity.

That's why I created a little helper class (called DataHelper). An instance of this class is instantiated in the activity which uses the data class and given to the method which uses the UI.

That looks quite complicated, but it's necessary in my app, since otherwise I had to make a bunch of callbacks and duplicate much code in every activitiy.

And that works.

Now the method which uses the UI (and is located in the helper class) has to be a resumable sub. So the method of the data class hat to be resumable, too.

An here I have a problem:
I can call the data call method which calls the helper class method. The InputList is shown, I can select an item. The helper class method seems to return (the log is shown), but the data class method never completes the Wait For statement.

Why? What have I done wrong?
 

Attachments

Creideiki

Member
Licensed User
Well... no.

I intentionally initialized the DataContainer instance in Main. In the real app the DataContainer contains much more data which does come from another activity.

It would be possible to create a new copy of the DataContainer in the calling activity (and implement a clone-method), but there are about 50 members I had to copy... that would cost much (time and memory), besides other flaws (e.g. changes have to be backpropagated).
 

Erel

Administrator
Staff member
Licensed User
1. Don't use the modal InputList. Use InputListAsync instead.

2. Consider removing the second activity and use B4XDialog instead. It will make things simpler.

3. The problem is that the DataContainer instance is paused together with the Main activity. There is no reason that the UI class can't be instantiated multiple times. All the data should be moved to a different class that is initialized in the starter service.
 

Creideiki

Member
Licensed User
ad 1. Yes, I know... I don't like the Async variants very much, since I have to use Wait For in the complete calling tree. It makes it difficult to write clean code since it adds much noise.

ad 2. It seems I have to look into B4XDialog soon. Is there a complete tutorial available?
In my case it won't be an option however... the activities in my app are quite complex and won't work well as dialogs i think.

ad 3. Hrmpf... wouldn't it be possible to create pure, neutral object classes...? I like how B4A hides many of the mechanisms of android, but in these cases I hate it... it makes it very difficult to abstract anything from the UI when there are such dependencies.
BUT - the idea with the Starter is a good one. I put the following code in Starter:
B4X:
Public Sub DataFab As DataContainer
   Dim d As DataContainer
   d.Initialize
   Return d
End Sub
Now I have to call CallSub(Starter, "DataFab") every time I need another object and that seems to work!

Thanks Erel.
 

Erel

Administrator
Staff member
Licensed User
It seems I have to look into B4XDialog soon. Is there a complete tutorial available?
https://www.b4x.com/search?query=B4XDialog

wouldn't it be possible to create pure, neutral object classes...?
The issue here is related to the fact that UI elements must run in a specific activity context.

The solution is simple. Separate the UI from all other stuff. The UI classes should be thin and easy to recreate. The logic and the program state should be managed in classes that belong to the starter service.
 

Creideiki

Member
Licensed User
:rolleyes: oh well... search engines are great ;)

This separation and the assignment to the Starter is the way I'm trying at the moment. I hope I get it working this way.

Thanks.
 
Top