call intent with callsubdelayed

Eduard

Active Member
Licensed User
Longtime User
I start some activities with an intent to parse extra variables to sub activity_create. This works perfect. Intents are 'bounded' to the activity, so even when the activity is re-created, the intent is still available.
B4X:
Dim Intent1 As Intent
Intent1.Initialize("","")
Intent1.SetComponent("com.mydomain.myapp/.myactivity")
Intent1.PutExtra("id", id)   
StartActivity(Intent1)

Now I want to execute an extra sub afterwards.

I tried to use
B4X:
Callsubdelayed2(Intent1,"name_of_sub","showmodel")
java.lang.RuntimeException: java.lang.ClassCastException: android.content.Intent cannot be cast to java.lang.String

It doens't seem to work. Should this be possible?
 

Eduard

Active Member
Licensed User
Longtime User
No. CallSubDelayed expects a module, not an intent.

You do not need the intent at all. Instead you should pass the data in the call.
I know it works without intent. But then I can't use intent flags and intent extra's to start the activity. Both are needed, so i need to start the activity as an intent.

I'll use a extra variable parameter as a workaround.

Thanks.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
The intent and CallSubDelayed are doing the same thing. You can do the same thing with either one.
1. If using intents only, Pass the "name_of_sub" in the intent extras and callsub from within the started activity.
2. If using CallSubDelayed, Use CallSubDelayed to pass all the intent parameters with the sub.
 
Upvote 0

Eduard

Active Member
Licensed User
Longtime User
The intent and CallSubDelayed are doing the same thing. You can do the same thing with either one.
1. If using intents only, Pass the "name_of_sub" in the intent extras and callsub from within the started activity.
2. If using CallSubDelayed, Use CallSubDelayed to pass all the intent parameters with the sub.
Yes CallSubDelayed can do some simular things as intent, but there are important differences.

Callsubdelayed calls an extra sub after activity resume, but does this only once. If activity is paused and later resumed, this extra sub is not called again. Furthermore, in activity_create and activity_resume is it not known if the extra sub is called later on or not.

With intent, this is different. The intent extra remains available and is already available in acitivity_create and in activity_resume. If I use an extra to call a sub, this extra sub is called again if you for example rotate the device, because the intent is still there.

Another difference is the use of flags.

With intent you can use FLAG_ACTIVITY_CLEAR_TOP to finish all activities if user goes back to a home activity. If I use CallSubDelayed, the activity stack is not cleared.

And another difference is that callsubdelayed calls the sub even if activity is in foreground already. With the use of an intent this is not the case. Activity is not started again, only the intent is changed. The extra sub would not be called in this situation, because activity_resume and activity_create is not called.

And last but not least, from notifications, intent is the only way.

So normally I use intents instead of callsubdelayed. But in a particualer case I callsubdelayed would suit me more. But the side effect: clearing the starting intent was not good. Therefore I tried to combine them: callsubdelayed(intent). I would have been great.

I figured a way though. With a global parameter, and use of callsub if the activity is already running, and intent if it's not running.

Thanks all for your help.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Hmm...you never described your usage in your original post, therefore it is impossible to answer your specific case.

In any case, you should be able to use both the Intent and CallSubDelayed (with the module name) together, I dont see any issues.

From my testing, I remember Intents do not remain 'bounded' to the activity. If you cycle out the app and resume if from elsewhere, it is possible to lose the intent. See this scenario: java - Losing Intent extras when returning to Activity - Stack Overflow

I would suggest you cache your intent.

Anyway, you would know better what is good for your usage case (which you havent described).
 
Upvote 0

Eduard

Active Member
Licensed User
Longtime User
Thanks,

I check if the activity has an intent in activity_create and if not create nothing and call activity.finish instead.
 
Upvote 0
Top