does onCreate fire any listener - android - android

I am creating a android library, In that I want to find if the OnCreate of the main application is running? . I dont know whether oncreate fire any listener. Anybody knows will oncreate fires any listener. Any ideas? Thanks.

No this callback does not exist.
The way most libraries introduce this callback is by making a base type that they require another application to call back into. Like you could make your library do something like:
public class MyLibBaseApplication extends Application {
public void onCreate() {
super.onCreate();
// my callback stuff.
}
}
and then require all the users of your library either use or extend your Application object as the Application class in their manifest. This way sort of sucks for developers though, although there are a few libs that do this. It blocks you from using multiple libs with this same pattern (Super annoying).
Personally I think just asking for a callback from whoever is using the library is probably much better from a client and dev who would integrate a library.
Just a simple call or static function that would be like:
public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
//register
YourLibrary.onCreateFired(this);
// or istantiate and register
YourLibraryCallback cb = new YourLibraryCallback(this);
cb.onCreateFired();
}
}
This way I think offers the most flexibility.

Related

How does Firebase.setAndroidContext(this) work and what does it do?

Could someone please explain how the following code snippet works?
public class FireBaseApp extends Application {
#Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
}
}
Context is a class that can be used to do Android and app specific things.
If you would like to see methods of the Context class look here
Firebase needs access to those methods in the scope of your app, that's why you need to set it.
In your case you can call Firebase.setAndroidContext(this); with the this argument, because the Application class is a instance of the Context class.
It initializes the Firebase library with the Android Context.
From Firebase documentation:
The Firebase library must be initialized once with an Android Context. This
must happen before any Firebase reference is created or used.
A Context according to Android Developers:
It allows access to application-specific resources and classes, as well as
up-calls for application-level operations such as launching activities,
broadcasting and receiving intents, etc.
I hope this explains well.

Moving code from Android's MainActivity class to their own Library or File

I am new to Java & Android development, but I have been able to create a mess of functions, subclasses & constants that work perfectly fine when pasted inside the MainActivity class of a default Android project.
The purpose of these functions is to "ping home" to let my servers know when an application install has occurred. The only code that is needed to make this happen is the line notifyMyServer() inside this standard block of code of MainActivity.java:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notifyMyServer("install"); /* Notify MyServer of an Installation */
}
}
Pasting the functions, subclass, constants and imports that make notifyMyServer() work directly inside the MainActivity.java file seems dirty to me. I would love to be able to move them to their own file and/or library so I could drag & drop that file into other apps I want to do perform the same tracking on.
How is this typically done?
There are at least two ways to solve this:
1) Create a NotifyActivity class which can be extended by MainActivity, as described in #Tim's answer. This uses inheritence.
2) Create a NotifyServer class and create an instance of it inside of MainActivity, either as a class variable or as a local variable in onCreate(). This uses composition.
Generally in Object Oriented Design, composition is preferred over inheritence. I strongly suggest that you learn about both of these concepts as you will use them over and over in your Java programming.
Take your notify code and create an class which just performs this notification (can be part of a library or not):
public class NotifyingActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
notifyMyServer("install"); /* Notify MyServer of an Installation */
}
}
And then in your MainActivity, just inherit from NotifyingActivity instead of Activity. Then the notification will automatically happen when you call super.onCreate() inside of MainActivity.

Android: how to send notifications to many activities

Inside my Application class, I have a long-running function (e.g. downloading new data). Once the operation finishes, I want to inform several related activities to refresh their UI. What is the best way to achieve this in Android (the equivalent of postNotification in iOS)?
public class MyApplication extends Application{
public void onCreate() {
downloadDataInBackground(); // call operationDone when finished
}
private void operationDone(){
sendMessageToAllRelatedActivities();
}
}
You can use: Broadcast Receivers for this. Please see following link:
http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html
I think you should use the Observer design pattern in order to notify that a certain status is changed
Observer

Google Analytics, what is the best way to use (implement) it from resource/programming perspective?

I am trying to use this feature (Google Analytics) in my app and can't figure out what is the best way to do it.
In first place I began to implement this in each Activity of the application.
public class MyActivity extends Activity {
public static GoogleAnalyticsTracker tracker;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tracker = GoogleAnalyticsTracker.getInstance();
tracker.start("UA-YOUR-ACCOUNT-HERE", this);
tracker.trackPageView("/myActivityView");
}
#Override
public void onDestroy() {
// Stop the tracker when it is no longer needed.
tracker.stop();
super.onDestroy();
}
}
But now I am thinking it would be better to create just one tracker at Application class and reference it from every Activity instead of repeat this code in each Activity I do create.
Can anyone explain which way is better and why?
Thanks.
This: Google Analytics in Android app - dealing with multiple activities
is a good read for coming up with a strategy for using Google Analytics in your Android apps.
In essence, you shouldn't call start()/stop() per activity because start() causes a new visit to be logged which isn't what most people intend or expect. However if this is the behavior you want, you are free to do it that way.
As Lukas said using a singleton or as you said using the Application context to call start()/stop() will get you more accurate tracking, but there are caveats when calling stop() that you need to be aware of. The link above goes into more detail.
Why don't you create a class for your tracker-functions and create and make it a singleton?
This class can then hold all the functions you need (like tracking the page view) and do all the background work for you.

How to always run some piece of code in android regardless of which activity or service run first in android?

I would like to run a piece of code every time any activity or service is started. In this piece of code, I might do things such as set the default uncaught exception handler and manage log levels.
The problem is that I have an activity which starts with the user clicking the application icon. I have another which starts if a certain intent is broadcasted, possibly from another app and possibly called before the user click the launch icon. Same goes for services.
I need to guarantee that a certain piece of code will be run while keeping the code clean; that is to say, without having to manually add that snippet of code to every activity and service class that I have.
Could you not extend the basic Activity class for Android like this:
public class MyClass extends Activity {
public void onCreate(Bundle bundle) {
//Add custom code here
}
}
Then have all of your actual "Activity"'s in your application extend the custom class?
public class MyInterfaceClass extends MyClass {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
//Other code here
}
}
That way all your custom code will be called when the Activity starts up.
For an application called Wibble...
public class Wibble extends Application {
protected static void DoSomething()
{
// Do your common code here
}
}
Then extend Activity...
public class WibbleActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Wibble.DoSomething();
}
}
Then derive all activity classes from WibbleActivity...
public class Whatever extends WibbleActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// WibbleActivity calls Wibble.DoSomething()
// so the 'Whatever' class doesn't have to.
}
}
Any Activity derived from WibbleActivity will run Wibble.DoSomething() implicitly.
If you want Services in the mix I can't help - I'm an Android 'rookie' and haven't got on to Services yet but I suspect extending your own app-level Service class might work in the same way.
You could extend Application and do it in its onCreate() method.
You have two choices
A) You can manually add the code - it might be only two lines importing and instantiating something from a source file you copy in unmodified - to every separate component that you write. It will only be in your projects, not in other people's unless they do it too.
B) You can, after no small difficulty, learn to make your own custom version of android that automatically does this each time it starts up a suitable component, and install this customized version on developer phones or hacked consumer phones.
"started" is ambiguous - what are you referring to? onCreate? onResume?
In any case, your best bet is to have a separate class with a static method that does the code you are talking about, and you call that in every single onCreate (or onResume, whichever you need) of each one of your activities.
That, or you create your own Activity subclass and derive all your activites from it, and override onCreate (or onResume). All your onCreate/onResume implementations are required to call the superclass' implementation, so you're guaranteed to have your code caled.

Categories

Resources