I do have a activity, which I don't have access to (we're working at an AIR ANE). From this activity, the one we're working on is triggered, but we can't use onActivityResult, as we don't have access to the calling activity. How can I work around this? If I close the second activity via finish(), it's to late to send an event. If I sent it before, the underlaying activity is still paused, as I've understood. I thought about using an Handler with a postDelayed, but I think, the timing will make this approach messy. Is there something like didFinish(), that's called once the next activity has already resumed?
Any thoughts appreciated!
Thnx, Marcus
The easiest way would be to send a broadcast before calling finish() and to register a BroadcastReceiver where you want to be notified.
You can use toast,alert or even broadcast receiver before calling finish();
Related
I implemented a broadcast receiver to update one of my UI activities but my problem is this: when the back button is pressed by the user, android kills the broadcast receiver but when the home button is clicked, the state is retained. What am trying to achieve is retaining the state even after the back buttton is clicked-- onBackPressed() until the user explicitly calls finish(). Any pointers?
You shouldn't rely on your Activity being always accessible to receive the broadcast. Instead, use LocalBroadcastManager to attempt to deliver the broadcast to the Activity, and if it fails - store the update locally and apply it when the Activity is displayed next time.
You need to register your receiver in your onCreate() method and call unregisterReceiver() on onDestroy().
Edit:
You can see #Shiki's answer in here.
It does what you need.
For example, I started an activity, there is a button in the activity
And then I start the background service, this service will check whether the target activity is on foreground, then trigger the click event of that button.
Is it possible to do this?
Why would you trigger a click event of an activity rather than notifying the activity about its results? Try to use local broadcasts which are sent from the service to the activity. The activity registers for that broadcast and in its onReceive of the BroadcastReceiver you'll trigger your onClick method or any other method of your choice. The Receiver is registered in onResume and unregistered in onPause to guarantee that the activity is actually visible.
I would not recommend to use a direct dependency on your activity as this might cause IllegalStateExceptions if in any circumstance your activity is not started or visible at all.
I can strongly recommend you to use eventbus concept like "otto by square" in this case.
you will subscribe to event from the activity this will keep to modularity and will let you do this function
I'd like to pass some data from a foreground activity to another that is currently in background.
I know that I can use setResult()/onActivityResult() and startActivity()/getIntent() to send an Intent with data while an Activity opens/closes, but in this case this does not seem to be usable.
Is there any way other than using static methods or fields?
you can use sharedpreferences to store the data
I haven't done what you are trying to do, but I think you may want to investigate Handlers and message passing...
BroadcastReceiver (registering it in onCreate and unregistering it in onDestroy) was exactly what I was looking for.
In my application there's a main activity which might be show on the top or in the background, and there's a broast receiver who receive the alarm event.
My main activity and the receiver seems to be two instances, and I can't call a static member in main activity (e.g.listview) to update itself from the receiver. It this correct?
So, how do i notify my current main activity to update itself?
p.s. I don't want to show my main activity if it's not currently on top.
Thanks a lot.
One mechanism I have seen used to provide a good solution is a Broadcast Receiver. You can read more about the solution in the Common Tasks area of the android documentation.
Edit: Although, rereading your question, I'm not positive I have enough information. If your receiver is already in your activity, then you just need to use the runOnUi(Runnable) call from the Activity to allow you to update the UI.
I have an activity that launches another activity with startActivityForResult method. I would like to terminate the called one programmatically but I don't know how to do this since in onActivityResult() method I have no information about the called activity and I cannot call finish() on it. How can I achieve this?
Thanks
The launched Activity can finish self:
setResult(RESULT_OK);
finish();
Try finishActivity(requestCode). According to the documentation it lets you finish an activity previously started with startActivityForResult. And if there are multiple such activities with the same request code, all will be finished.
Note: I haven't actually tried this myself, but that's what the documentation says! Experiment with that, see if it does what you want.
At the moment you call startActivityForResult your Activity will be closed or paused and the new activity is started. The only one that can finish the new activity is the new activity.
You could start a background task and let this background task somehow notify your activity the activity could now finish itself.
I don't know if a Handler created in Activity A and passed to a thread will remain valid if Activity A is paused and Activity B is active. But I would assume this works because both of the Activities are running in the same thread therefore they should share the same message queue.
This thing just bit me, so I thought I'll add a comment here:
if(readyToFinish()){
finish()
}
thisCodeWillBeExecuted()
My experience is that all your code in the stacktrace gets executed, before
the activity is finished. The documentation is not ideal at this point.