i want to so some changes in the application at run time . whatever the changes i want to do that must be done with in two steps. one step i performed in the attachBaseContext() method and another step i have to perform in a method that call before the onCreate() method of the ContentProvider . so
can any body tell me that which method i can override in my application class which calls before the onCreate() method of ContentProvider (other then attachBaseContext() ) .
Well, of course the constructor will be executed first.
However, if you really need a commit to android.content.Context, I recommend using attachBaseContext. Although at the moment, i have not found any documentation mentioning attachBaseContext in the lifecycle of android.app.Application. But my recent tests are that attachBaseContext is always executed before onCreate happens.
The answer is: attachBaseContext()
Related
in my app, there are 5 classes and 1 activity. This activity has, as you all know, an OnDestroy() method. In this method I need to remove a test provider which is set up in another class called "mockingclass".
In "mockingclass" I have a method similar to this:
public void mocker()
{
xxx
location.RemoveTestProvider(location.GpsProvider);
xxx
}
While xxx stands for many other functions in this method, when the app is being destroyed I need to call ONLY for that ONE function in within this whole method.
Is there any way to do that at all? If not, what would be your suggestions?
THANKS!
Obviously you can't choose a single line from method to be executed. You need to extract it separate method which will be called separately. Maybe you should look at some injection? Let this call be injected as an delegate for example. In that way you could manipulate what should be called depending on situation.
I need to call getPackageName early in the application lifecycle. I tried to call it in the Application constructor only to see that it throws NullPointerException. I had a look at the Android source code and found that Android calls the internal attach method which in turn calls the documented protected attachBaseContext method. Once I moved my code from the constructor into attachBaseContext everything works as expected.
Question: is it good idea to assume attachBaseContext method as a kind of extension to the Application constructor?
If you need just pacakageName I would suggest to use BuildConfig#APPLICATION_ID, because it is static variable and doesn't require waiting for application initialization. Difference between package name and application id you can find here.
If you anyway need some entry point to application, IMHO it seems to be a good idea to use attachBaseContext(Context c) method, because:
It might be called only once (as constructor).
It is the first place in Application where you can get application context
I know onCreate , onListItemClick and onPageSelected etc. are callback methods in android.
Yet some are not so obvious to tell they are callback methods,
for example, getItem method in class FragmentStatePagerAdapter.
(Isn't it? Or getItem is not a callback method?)
How can we know a method is a callback method in android?
Is there any clue to let people know they are callback methods?
Or is there a list of all callback methods in android?
The documentation for each class at the Android developers site will list all of the methods available to you as a developer.
Take the Activity class for example. We can generally sort the methods into two large buckets:
Methods that are final such as getApplication()
Methods that are not final, such as onCreate()
Methods that fall into the first bucket you can immediately rule out from what you refer to as "callback" methods. Since they are final, you cannot override their behavior.
Methods that fall into the second bucket might be a "callback." I wouldn't get hung up on trying to label them as such though. Each and every method in the Activity class may or may not get called by some other piece of code at some point in time. However, most of those methods exist because they will get called at some point in time.
Even some of the methods that you might not think of as a "callback" method can be overridden and can be called by the framework (or your own code). For example, you can override the finish() method of an Activity. Most applications will only ever call finsh() directly without customizing the behavior, but you do have that option.
Focus on what you need to do, find the appropriate method to either call or override, then read the documentation on that method. Whether or not the method is a "callback" is usually irrelevant.
package com.example.mytests;
import android.app.Activity;
public class MainActivity extends Activity {}
For the code above, even though I do not implement OnCreate() method I get no errors and program works. Should't I get an error that I should override it?
No. You will get error if you override onCreate and forget to call super.onCreate first thing.
In your case just Activity.onCreate() gets called. It can't really give an error, unless they in Google break something severily which they do not. If it gave Android wouldn't work because the same error would come from super.onCreate()
No, you don't need to override it. But if you DO override it, you MUST call super.onCreate().
The idea is that no matter what, the default implementation of onCreate() must be called, which is also the case if you do not override it at all.
onCreate is one of the Life Cycle methods of the Activity
Reason we implement the onCreate because some where we need to set the content view to the activity. For that reason we choose onCreate for setContentView() and it is the first life cycle method to be called. and if you override the onCreate dont forgot to call the super.onCreate()
why super.onCreate
You are extending class which has already defined onCreate method so overriding indicates to use your behavior.otherwise super class behavior will b used.this is same as extend thread class and not override run method.here do nothing run method will b invoked when u call start method on thread . You might be confused in implementing and extending interface or class.
should I call super.xxxx() before my custom code or after when I override a method?
Is there some skills to Identify?
The short answer: It Depends.
The long answer:
You need to understand what the implementation of the method in the parent class does.
There are cases when the super method does some "initialization" work required for the overridden method to function smoothly. In such cases, the super.myMethod() call should be the first line in the method.
In some other cases, the implementation of the method in the parent class could be responsible for some clean up (or some sort of finalization) operations. Thats when you would need to make the call to super the last line in your overridden implementation.
Then there are situations where the order doesn't matter, but you must call the super method because the implementation in the base class performs some operation which are necessary; although the order of these operations doesn't matter. Most of the Activity life cycle methods fall in this category. Which is why you must call though to super.onCreate() although you can do it at any point in your onCreate().
Finally, there are also cases where the call to the super method is not required. The base class has a default implementation which you may choose to use or ignore. You might also end up doing an if-else here: For instance, you might call through to the super implementation. If it returns a null, you might create a new object and return that instead.
Is there some skills to Identify?
Read the method documentation and see if there is any mention of the order in which the super method must be called.
Go through the source code of the base class and see if you can figure out whether it does anything that necessitated calling it in a particular order.
(Very OOPy) generally, the idea is to call super just when you need a parent's functionality. I know it seems pretty obvious, but it has implications when it comes to cleanup functionality:
Init:
super.init
... your initialization code
Delete:
... your deletion code here <-- important
super.delete
SomeOtherRandomMethod:
... your code may go here if it does not need any state from the parent
super.SomeOtherRandomMethod
... otherwise it can go here
I find it's just simpler / cleaner to just call super's code at the beginning of an override (unless there's some really specific reason not to), and just make sure that I call the parent's destroy/delete/free/destruction after I've done my own cleanup (we definitely don't want the parent dumping the object out from under us!)