Android Question [Solved] About StartActivity

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
I would like to ask what happens every time we call StartActivity(ActName):
  1. Previous ActName is destroyed and a new ActName is started, so garbage collector releases the previous ActName?
  2. Previous ActName is simply paused and a new ActName is started?
Thank you in advance!
 

JordiCP

Expert
Licensed User
Longtime User
3. Starts an activity or brings it to front if already exists ;)

(start typing it in your IDE and this will appear)

screen.png
 
Upvote 0

vfafou

Well-Known Member
Licensed User
Longtime User
Hello Jordi!
Thank you for your response.
I really haven't noticed that the autocomplete comment describes the functionality of StartActivity!
One more question:
What could I do to start an activity from a service? This is critical for my app functionality!
Thank you in advance!
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Just call
B4X:
StartActivity(yourActivity)
However, unless your app really requires it, usually it is better to launch a notification and let the user decide if he wants to open the app or not by clicking on it (just as gmail, whatsapp or others do)
 
Upvote 0

vfafou

Well-Known Member
Licensed User
Longtime User
It is more complicated in my case:
I need to start an activity after receiving a push message, received from a service module while screen is off.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
There are some threads regarding this.

So, if you must launch for instance your main activity, just do it from your service
B4X:
StartActivity(main)
If it does not work when the screen is off, you could try this (not tested, taken from an old code where I did something similar with a transparent activity)
B4X:
      Dim In As Intent
     In.Initialize("","")
     In.SetComponent("my.package.name/.my_activity_name")    '<-- for instance "b4a.example/.main"

     Dim FLAG_DISMISS_KEYGUARD as Int = 0x00400000
     Dim FLAG_TURN_SCREEN_ON as Int   = 0x00200000

     In.Flags = Bit.Or(FLAG_TURN_SCREEN_ON, FLAG_DISMISS_KEYGUARD )
       
     StartActivity(In)
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Erel says somewhere in forum that you should not use StartActivity from a service.
Using CallSubDelayed is the right way.
You are right @DonManfred ;)

However, I doubt (well, the exact sentence would be "I don't know at all") if CallSubDelayed will have the same effect as a Starting intent (with the corresponding flags) in case the screen is off (will try it later just out of curiosity)
 
Upvote 0

vfafou

Well-Known Member
Licensed User
Longtime User
Thank you all for your advices! I have already used partial wakelock to the previous version of the app I'm writing by now and it really works great!
 
Upvote 0
Top