Skip to content

Instantly share code, notes, and snippets.

@amay077
Created January 21, 2011 09:24
Show Gist options
  • Save amay077/789461 to your computer and use it in GitHub Desktop.
Save amay077/789461 to your computer and use it in GitHub Desktop.
[Android]Android のアプリケーションを終了しても、static なオブジェクトは生存していることを確認するサンプルコード。
//////////////////////////////////////////////////
// AppBean.java
//////////////////////////////////////////////////
package com.amay077.android.statictest;
import android.app.Application;
public class AppBean extends Application {
private int id = 0;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
//////////////////////////////////////////////////
// MainActivity.java
//////////////////////////////////////////////////
package com.amay077.android.statictest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
/** static な変数 */
private static int id = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 起動→終了→起動を繰り返すと id が加算されていく。
Log.d("MainActivity", "static member:" + String.valueOf(id));
id++;
// Application の派生クラスでも結果は同じ。どんどん id が加算されていく。
AppBean bean = (AppBean) this.getApplication();
Log.d("test", "AppBean member:" + String.valueOf(bean.getId()));
bean.setId(bean.getId() + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment