I'm writing an NPM module to emit an event from native code after the MainActivity is started, so that I'll be able to add a listener in componentWillMount() and do the things I want. I can write the listener code somewhere else but I want to access the props of the component.
I'm sending the event right after startActivity call, as the startActivity call is asynchronous it immediately sends the event even before the component from JS part is initialized. So, I'm not able to listen for the event.
Ultimately, I want to wait till the MainActivity is started so that the listener will be added then it'll be safe for me to emit event from native code.
Any ideas or pointers how to do it?
I figured out a way to finally achieve what I wanted. I defined a public static function in my native module file and called it from onCreate function of my app's MainActivity.java.
I successfully published the module to NPM...:D
https://github.com/vicke4/react-native-invoke-app
Related
I am using Greenrobot's EventBus in my app, and it works fine.
However, if I press the back button to close the app, then restart the app instantly I seem to receive the event twice. If I then do so again, I will receive it three times and so on.
I am checking with logs and debugging to see if I have multiple instances of any classes, or if I am registering multiple times, but I can't see any extra classes and using isRegistered returns false.
Any ideas?
Thanks
Are your register/unregister calls paired correctly? E.g. if you register() in Activity.onResume(), are you calling unregister() in Activity.onPause().
Closing all activities does not kill your process. I.e. all registered classes are still there, you have to explicitly clean up and unregister from the event bus, or reuse them when the Activity comes back.
This is old, but just in case anyone has this problem also: Tread lightly when using EventBus inside dynamically generated things like Fragments or other classes; I didn't really understand why they were posting to the EventBus more than once, but I think it had to do with this (I had more than one dynamically generated Fragment). It worked normally once I put the register(), unregister(), onEvent() into the parent Activity code (which conveniently also uses onPause() and onResume()).
Same thing happening in my case when I am using the
EventBus.getDefault().postSticky(new Event("Hii !"));
for sending the event. The event is received multiple times when I come to that activity. So I fixed this by removing the event after receiving in onEvent method. This solved my problem. Used: removeStickyEvent(object)
#Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEvent(Event event) {
/* Do something */
EventBus.getDefault().removeStickyEvent(event);
}
The problem was not that the event was actually fired multiple times, but that the handler was invoked multiple times. As seen in the code above, the bus.register method is called everytime I create the object; because of the activities lifecycle, this happened multiple times, causing the handler to be invoked multiple times.
I had a specific case that I want to share. Maybe it helps someone else.
While we are using a parent activity for all of our Activities in our project, we register and unregister EvenBus for each activity inside the parent class.
In one of our activities, we were calling EventBas before invoking the previous activity's EventBus. Then we had twice trigger
I have a 'trick' that I do sometimes in Java/Swing in which I create a swing component that is always invisible and have property change event listener(s) attached to it. When a variable(s) changes value, the code will cause that component to change and fire an event and anything listening will know that those variable(s) have changed.
I want to do this same thing in Android or AndEngine. What would I use instead of the swing component? I'm was thinking of overriding some subclass of org.andengine.entity.Entity, but I don't see any event to fire.
Sounds like you almost hit on it with your Swing idea. There is no event pattern built into andengine, and the reason for this is probably because of the event classes built into the android operating system.
You should have good luck implementing a Listener (Observer Pattern) into your app following the steps in this other Stack Overflow answer:
Custom event listener on Android app
or this simple example in which the broadcaster call a function on a single listener:
Android - event listener
As a footnote, neither example includes code to REMOVE listeners. Be sure your implementation does include that so that you can garbage collect objects you don't need any more.
I have a question in android.
If my activity class interact with external library. This library which I'm working on it has to do some processes when the application go into background or when it returns to foreground.
Is there any way to know when the application enters the background.
NOTE: I don't want to do anything in the activity. I don't want to send callback to the library when onPause is invoked.
Thanks
If I understood your problem, you could use Application.registerActivityLifecycleCallbacks and implement your own Application.ActivityLifecycleCallbacks.
Something like:
class myActivityLifecycleHandler implements ActivityLifecycleCallbacks{
// Methods implementation here
}
yourActivity.getApplication().registerActivityLifecycleCallbacks(new myActivityLifecycleHandler())
You may write in some file when application coming to background.
Then your library may time for time checks this file.
Application.registerActivityLifecycleCallbacks is indeed the correct api that you want to use. Unfortunately it is indeed API14+, so you have the following option:
use this library to implement the activitylifecycle callback. You will have to extend your activities in the application you are working on, but something must be done to then no matter what as you need less than API14 support.
With this library implemented, you can create an int count in the callback. This count can be incremented and decremented every onStart() and onStop() callback for each activity.
The count will go from 0 -> 1 on app open, and 1 -> 0 on app close/background. When these conditions are met you can do calls to your library.
Remember that when you background your app the OS can kill it off any time. If you have any amount of networking or anything long, start a service to handle it all as quickly as possible.
override onPause method of activity, onPause is called when activity is going to backgroud
Add a logging to your Droid1 project. Within the onCreate() callback method, add an informational logging message, using the Log.i() method. Run the application and view the log results.
[For example, where would I type "onCreate()"? In the manifest? and under what
Implement some of the Activity callback methods in addition to onCreate(), such as onStart(), onRestart(), onResume(), onPause(), onStop(), and onDestroy(). Add a log message to each callback method and then run the application normally. View the log result to trace the application life cycle. Next, try some other scenarios, such as pausing or suspending the application and then resuming. Simulate an incoming call. Watch the application log to see how the activity responds to such events.
[ I understand how to simulate calls through the DDMS and etc., but other than that, I am clueless on where to type and of that code, and what variables if any to include.
I am running the latest Android SDK and using Eclipse.
EDIT: No this is not homework, this is from the Sam's Teach Yourself Book
EDIT(Revised): Once I get this..
public void onCreate(Bundle savedInstance) {
... code ...
}
where do I post this, in the Manifest.xml? under activity such as < activity >?
Android SDK uses Java as programming language. If you're familiar with Java you should start reading this:
Android Dev Guide
An activity in android SDK its (copy and paste) "[...]a single, focused thing that the user can do[...]". Here it is the whole explanation:
Activity guide
Good luck with your exercise :)
When you create a new Android project you will start out with a template activity, usually called MainActivity.java.
Inside this file you will see:
public void onCreate(Bundle savedInstance) {
... code ...
}
This is the onCreate() method. You can change the ... code ... part and try to use code from your book instead.
After you've played around with that a bit you can move on to more complicated code examples, such as the Samples section of the sdk.
I started to use in app messaging. After the first time it didn't work. When I look to logs it says display event listener removed (just after after opening the app).
2019-11-07 10:44:26.800 973-973/ I/FIAM.Headless: Setting display event listener
2019-11-07 10:44:27.460 973-973/ I/FIAM.Headless: went foreground
2019-11-07 10:44:28.690 973-973/ I/FIAM.Headless: Removing display event listener
When I recreate app with( recreate () ) method. It works well. I am using splash screen in my app. As far as I understand activity life cycle used by the SDK to decide when to add or remove display listener. I am not sure if the problem is associated with the beta version of the in-app messaging.
Any help is appreciated.
I was facing same problem, I fixed it by following below steps:
1. Do not call finish() while starting the MainActivity.
2. Start MainActivity using startActivityForResult(intent, 100).
3. onBackPress() of MainActivity call setResult(100) before call finish().
4. In onActivityResult() of SplashActivity check if response code is equal to 100 then call finish().
Please up-vote if you found helpful to you.