9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

android.app.Application クラスを拡張する

Posted at

Manifest の <application に、android:name で、android.app.Application の拡張クラスを定義

Manifest.xml
    <application android:name=".MyApplication" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"></application>
MyApplication.java
public class MyApplication extends android.app.Application {

	private int myField;

	public void setMyField(int field){
		this.myField = field;
	}
	public int getMyField(){
		return this.myField;
	}

}

使う時はこんな感じで使う。

MainActivity.java
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set my activithy to Application context
        MyApplication app = (MyApplication)getApplication();
        app.setMyField(1);
        
    }
}
9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?