Trigger my app when another app closed - android

I launch another app from my application.
Is there any way to trigger my app when launched app closed?!
Is it good idea to use timer and check package name ?!

Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. To receive a result, call startActivityForResult() (instead of startActivity()).
For example, your app can start a camera app and receive the captured photo as a result. Or, you might start the People app in order for the user to select a contact and you'll receive the contact details as a result.
Of course, the activity that responds must be designed to return a result. When it does, it sends the result as another Intent object. Your activity receives it in the onActivityResult() callback.
More info here.

If you launch the app with startActivityForResult, you will end up in onActivityResult in your application when it closes, that is the normal flow.
But if you want to always launch your app when the other closes, it can be done using a service, but I wouldn't recommend doing that since it's bad practise.

Related

Is using an invisible activity a bad practice?

I'm working with several applications.
To log in we uses Oauth2 with customtabs using chrome.
The "mother" application performs a login then send an intent to every application to say "perform login a preload your data in background"
From there each application opens a customtab and close it, then do its other tasks in background.
yes I know, this is ugly, 6 customtabs are being opened and closed, but it is what was asked, no other solution is wanted.
To do that I made an activity without UI.
The activity receive the intent, launch the customtabs, retrieve the authorisation_code, get the tokens, then call a service to preload data and close itself (the invisible activity close itself)
Is that bad?
I did this because it's impossible to add an intent filter to an intent service, and my chrome customtabs send a intent when it's done. so only an activity can grab it.
Thanks.
I'm guessing you are developing a multiflavored app.
It's perfectly fine to use a deeplink activity to receive the intent to retrieve the code
and finish it directly in oncreate

Where should I be using getIntent() in the Android lifecycle

I have a bug in my app which I thought I knew how to resolve, but now I've thought more about what is happening I'm not sure I do know the fix.
My app receives an incoming intent from a calling third party app as a string. That string can be sent as a SEND intent or a VIEW intent.
All of that works fine unless the app is already running...
This is what is happening:
My app is not running (not listed in the running apps view)
Share is clicked in another (third party) app and my app is selected to receive the shared text (text1).
My app opens and the text is displayed (and processed) as expected.
The user switches back to the third party app and shares some different text (text2) and my app is selected to receive this new text.
My app opens, but the original text (text1) is still displayed.
At this point I thought that the bug was because I am reading the intent in onCreate() and then displaying and processing it. My thinking was that as the app is already running onCreate() is not being called when the app is shown the second time as we jump into the lifecycle at onResume().
However, if I continue the test as follows:
Without exiting my app the user switches back to the third party app again and again shares the same piece of second text (text2) with my app.
My app is displayed again but this time correctly shows and processes the second text.
How can this be, as the app is still running, surely onCreate() is still not going to be called!
I thought that the fix was going to be simply to move the getIntent() call into onResume() (or onStart() ?) But now I'm not sure about this. Is this right thing to do?
The core of the issue is the fact that your Activity is already on top of the activity stack when the second Intent is being fired.
The launch mode will matter here.
What you can do is, set the launchMode to singleTop, and then get the intent in onNewIntent() method.
That is a good way of handling such scenarios. Please read the link above and it will make things very clear.

Adding parameters to app start chain from notification

I am using GCM to send push notifications to my Android application and when the user clicks the notification, they should be taken to a specified view in the application (For context: It's an event log).
I understand that in the simplest case, I just add some parameters to the intent that is starting the app.
However, my app requires some remote data to be available when entering my MainActivity. When starting the application normally, it's pointing to another activity, let's call it AppStartActivity. It's showing a Splash screen while downloading the neccessary data and launches MainActivity when it's ready.
When starting the app from a notification, the neccessary data may or may not be ready, depending on whether the app is currently in background (and if it has not been garbage collected). Therefore, I think I have to launch AppStartActivity when clicking the notification, and then somehow forward the Intent parameters and I'm not sure it's the right way. I have formulated some questions about this:
Is it correct to lauch AppStartActivity with the parameters I will need in MainActivity later and then just forward them when switching to MainActivity?
Is there a simpler way to do this, such as finding out the state of the application when clicking the notification and then choose either AppStartActivity or MainActivity depending on if the neccessary data is available? The reason I'm asking is that when I create the notification, the data may or may not be available, but it may change during the time passing before the user to interact with the notification. I understand that I can launch a new activity that is just checking the state and then launch either MainActivity or AppStartAcitivty, but it seems like it could be a bit too complicated...
I'm thankful for any input on this matter. Please let me know if I have left out some details.
as I understand it, I had a similar situation, and this was my solution:
when I receive the GCM, my onReceive method starts a service
for getting the remote datas. Only when these datas are
avaiable in a local content provider, I throw the notification to the user.
[edit]
You have to manage any network error with some notice, otherwise you may lose the GCM notification

How to start an Activity from a dialed phone code (e.g. #120)

I am trying to launch my app from a dialed code (say #120) on the phone dialer. I believe I've seen some apps that do this already.
So to launch the app, the user would go to the phone and try to make a call to #120, which would open my activity instead of making a phone call to #120.
Anyone with ideas on how this can be accomplished?
By registering an intent in the chain of responsibility for the dialer-action. The chain of responsibility is ran down from first to last entry, and each registered observer may choose to either handle the entry or pass it down to the next entry in the chain. That way, you could handle the code with one application and let the other applications handle the phonecall.

Android can ACTION_SEND be used to call a Service

I've successfully being able to use the ACTION_SEND intent which displays my application in the list when the share button is pressed in the gallery. My app basically sends the selected picture to my computer and no further input form the user is required. Rather than starting my app when selected in the list which is an unnecessary step is it possible to pass the data to my own service which sends the picture and avoid starting my app?
If not what other options do I have? Start my app, sent the picture and close the activity?
Rather than starting my app when selected in the list which is an unnecessary step is it possible to pass the data to my own service which sends the picture and avoid starting my app?
ACTION_SEND is used for starting activities, not services -- sorry.
If not what other options do I have? Start my app, sent the picture and close the activity?
Sounds reasonable. If you do all of that in onCreate(), call finish(), and never call setContentView(), nothing will be shown to the user.

Categories

Resources