0

I am trying to use this solution inside my Fragment, however I couldn't be sure where to call System.LoadLibrary(), finally I decided to call from onCreate method of the Fragment, I want to be sure if this would not raise some another weird error on different devices.

Community
  • 1
  • 1
user65721
  • 2,833
  • 3
  • 19
  • 28

2 Answers2

1

It is just a static initialization. You don't even have to have it inside your onCreate() at all. You can simply initialize it as the first part of your class next to your global variables as such:

class YourClass
{
   static
   {
       System.loadLibrary( yourLib );
   }

   ...
}

It is only important that it is done early or you may see UnsatisfiedLinkError followed by application's crash. That way whenever a call referenced later is made, the library itself has already been loaded. A static declaration at the top will do that as soon as an instance of the class has been requested; even before onCreate().

Toochka
  • 894
  • 1
  • 9
  • 25
Jay Snayder
  • 4,298
  • 4
  • 27
  • 53
  • As I clearly stated on my question, I need to access to the context when I call the loadLibrary because of this solutoion http://stackoverflow.com/questions/18111739/why-do-some-android-phones-cause-our-app-to-throw-an-java-lang-unsatisfiedlinker/24296556#24296556 – user65721 Jan 12 '15 at 19:09
  • @user65721 You didn't state that in your question. You could do it earlier, but if the fragment is the first place you will be using the library then there shouldn't be an issue doing it there. – Jay Snayder Jan 12 '15 at 19:16
1

General it won't cause weird behaviours, as onCreate is mandatory callback when your fragment created by system. This make sure your native library is prepared before you do further operation in your fragment.

Take a look at fragment life cycle, it might be better if you put your library initialize routine on the first callback onAttach, you can use the attached Activity context to initialize your native library.

alijandro
  • 11,627
  • 2
  • 58
  • 74