question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android - onActivityCreated never called on detached project

See original GitHub issue

I’m trying to use https://github.com/wix/react-native-keyboard-input in my detached android (exp detach) project and it’s not working but the project that created by $ react-native init does. So I diged into the android project and found out that the onActivityCreated method of Application.ActivityLifecycleCallbacks never called. Here the code:

public class KeyboardInputPackage implements ReactPackage {
   public KeyboardInputPackage(Application application) {
     application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                sCurrentActivity = activity; // never trigger
            }

            @Override
            public void onActivityStarted(Activity activity) {
                sCurrentActivity = activity; // trigger when I turn off the screen then turn on again
            }

            .....
     })
   }
}

The onActivityCreated never trigger make the module entirely not working. How can I fix it?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
khacanhcommented, Nov 11, 2017

The problem is that in Expo app (even after detaching)

public class MainActivity extends ExponentActivity {
  ...
}

ExponentActivity extends from ReactNativeActivity, which is something defined by Expo SDK.

Meanwhile, apps created with react-native init has

public class MainActivity extends ReactActivity {
  ...
}

The way ReactNativeActivity handles activity events are very different from ReactActivity. It will not call the methods defined and passed in addActivityEventListener

That means, the official way to create call back in android bridge, as it is documented here https://facebook.github.io/react-native/docs/native-modules-android.html, will no longer work.

I think this is a big issue as all of the bridge packages that needs callback will not work. Would be great if the author can help address this.

Thanks.

2reactions
jesserudercommented, Jan 17, 2018

This should be fixed in SDK 24!

Read more comments on GitHub >

github_iconTop Results From Across the Web

onActivityCreated is always called? - Stack Overflow
Most methods of the host Activity are mirrored by Fragments. onRestart() called when your Activity in a back stack and you hit back...
Read more >
Bountysource
Android - onActivityCreated never called on detached project.
Read more >
Fragment lifecycle - Android Developers
The onDetach() callback is invoked when the fragment has been removed from a FragmentManager and is detached from its host activity.
Read more >
Deprecate Fragment's onActivityCreated() [144309266]
We should deprecate onActivityCreated(), citing that the method is called directly after onViewCreated() and is not a one time call immediately after the ......
Read more >
core/java/android/app/Fragment.java - platform/frameworks/base ...
<li> {@link #onActivityCreated} tells the fragment that its activity has ... is called when this Fragment is attached or detached when not popping...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found