Using the Application Class
Depending on what you're doing in your initialization you could consider creating a new class that extends Application
and moving your initialization code into an overridden onCreate
method within that class.
public class MyApplicationClass extends Application {
@Override
public void onCreate() {
super.onCreate();
// TODO Put your application initialization code here.
}
}
The onCreate
in the application class is only called when the entire application is created, so the Activity restarts on orientation or keyboard visibility changes won't trigger it.
It's good practice to expose the instance of this class as a singleton and exposing the application variables you're initializing using getters and setters.
NOTE: You'll need to specify the name of your new Application class in the manifest for it to be registered and used:
<application
android:name="com.you.yourapp.MyApplicationClass"
Reacting to Configuration Changes [UPDATE: this is deprecated since API 13; see the recommended alternative]
As a further alternative, you can have your application listen for events that would cause a restart – like orientation and keyboard visibility changes – and handle them within your Activity.
Start by adding the android:configChanges
node to your Activity's manifest node
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
or for Android 3.2 (API level 13) and newer:
<activity android:name=".MyActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name">
Then within the Activity override the onConfigurationChanged
method and call setContentView
to force the GUI layout to be re-done in the new orientation.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.myLayout);
}
From the Android Developer Documentation:
-
px
> Pixels - corresponds to actual pixels on the screen.
-
in
> Inches - based on the physical size of the screen.
> 1 Inch = 2.54 centimeters
-
mm
> Millimeters - based on the physical size of the screen.
-
pt
> Points - 1/72 of an inch based on the physical size of the screen.
-
dp or dip
> Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160
dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
-
sp
> Scaleable Pixels OR scale-independent pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommended you
use this unit when specifying font sizes, so they will be adjusted
for both the screen density and the user's preference. Note, the Android documentation is inconsistent on what sp
actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".
From Understanding Density Independence In Android:
Density Bucket |
Screen Density |
Physical Size |
Pixel Size |
ldpi |
120 dpi |
0.5 x 0.5 in |
0.5 in * 120 dpi = 60x60 px |
mdpi |
160 dpi |
0.5 x 0.5 in |
0.5 in * 160 dpi = 80x80 px |
hdpi |
240 dpi |
0.5 x 0.5 in |
0.5 in * 240 dpi = 120x120 px |
xhdpi |
320 dpi |
0.5 x 0.5 in |
0.5 in * 320 dpi = 160x160 px |
xxhdpi |
480 dpi |
0.5 x 0.5 in |
0.5 in * 480 dpi = 240x240 px |
xxxhdpi |
640 dpi |
0.5 x 0.5 in |
0.5 in * 640 dpi = 320x320 px |
Unit |
Description |
Units Per Physical Inch |
Density Independent? |
Same Physical Size On Every Screen? |
px |
Pixels |
Varies |
No |
No |
in |
Inches |
1 |
Yes |
Yes |
mm |
Millimeters |
25.4 |
Yes |
Yes |
pt |
Points |
72 |
Yes |
Yes |
dp |
Density Independent Pixels |
~160 |
Yes |
No |
sp |
Scale Independent Pixels |
~160 |
Yes |
No |
More info can be also be found in the Google Design Documentation.
Best Answer
Android is built on top of Linux, and a large portion of it is written in C. There are swaths of code written in C++ though. Also, most of the application framework (the stuff you would call android, and almost everything you see on screen) is written in Java.