Created
January 21, 2011 09:24
-
-
Save amay077/789461 to your computer and use it in GitHub Desktop.
[Android]Android のアプリケーションを終了しても、static なオブジェクトは生存していることを確認するサンプルコード。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////// | |
// 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