I was in a similar situation. I needed to execute the method only once, but the onCreate() , onStart() and onResume() methods did not work for me, because these methods are called when the device is rotated and in other situations.
So, I decided to extend Application and run this method in onCreate() my user-defined application class, because it is executed only once when the application starts and because the task does not require a long
Here is an example:
public class CustomApp extends Application { public CustomApp() {
The next step is to say Android, we have our own class of applications. We do this by referencing the custom application class in the "android: name" attribute of the applcation tag. Like this:
<manifest ... <application android:name="com.package.example.CustomApp"> <activity> </activity> ... <activity> </activity> </application> </manifest>
... For those who can help you!
source share