Skip to content

Instantly share code, notes, and snippets.

@dnkm
Last active August 29, 2015 14:18
Show Gist options
  • Save dnkm/a6c3c1301c4658ac73b6 to your computer and use it in GitHub Desktop.
Save dnkm/a6c3c1301c4658ac73b6 to your computer and use it in GitHub Desktop.
Android Tutorial - Chapter 3 Activity
<!-- Declaring the main "launcher" activity -->
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
TextView mTextView; // text view in the layout
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set UI
// e.g.: res/layout/main_activity.xml
setContentView(R.layout.main_activity);
// set up member variables
mTextView = (TextView) findViewById(R.id.text_message);
// version checking
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setHomeButtonEnabled(false);
}
}
@Override
public void onDestroy() {
super.onDestory(); // ALWAYS call this
// TODO: destroy background threads (otherwise, this method overriding is often optional)
}
// 1. stop animations and other CPU-consuming actions
// 2. commit unsaved changes
// 3. release resources (receivers, handles, sockets..)
@Override
public void onPause() {
super.onPause(); // always call this first
if (mCamera != null) {
m.Camera.release();
m.Camera = null;
}
}
// kind of counter-acts the onPause() method
@Override
public void onResume() {
super.onResule();
if (mCamera == null) {
initializeCamera();
}
}
// automated restoration usually does its job... i.e. don't need to implement
@Override
protected void onStop() {
super.onStop(); //again, call this first. always.
ContentValues values = new ContentValues();
values.put(SOME_TITLE, getTitle());
values.put(SOME_VALUE, getValue());
getContentResolver().update(
mUri,
values,
null,
null
)
}
// use onStart() to counter onStop(), rather than onRestart().... in most cases
@Override
protected void onStart() {
super.onStart();
if (!gpsEnabled) {
enableGPS();
}
}
// when system destroys an activity due to system contraints
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt(PLAYER_SCORE, mCurrentScore);
super.onSaveInstanceState(savedInstanceState); // always all it at the end
}
// restoration - option 1
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mCurrentScore = savedInstanceState.getInt(PLAYER_SCORE);
} else {
// put default values..?
}
}
// restoration - option2
// this method runs after onStart()
// this method runs ONLY if there's a saved state to restore
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mCurrentScore = savedInstanceState.getInt(PLAYER_SCORE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment