4
5

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 ] Applicationを継承したクラスを使う

Posted at

以前、セッターを使って異なるクラス間で値をやり取りする方法を紹介した。
https://qiita.com/QiitaD/items/50fb0b6b7709e66a5041

今回はApplicationクラスを継承したクラスを作り、そのクラスを介して異なるクラス間で値をやり取りする方法を紹介する。

クラスの作成と登録

以下のようにApplicationクラスを継承したクラスを作る。

public class MyApplication extends Application{
    public static String test = "test";
}

しかしこれだけでは使用できない。以下のようにしてAndroidManifestに登録する必要がある。

<application>
        android:name=".MyApplication"
</application>

使用方法

MyApplicationクラスは以下のように使用する。

//アプリケーションを取得し、MyaApplication型変数に代入する
MyApplication myApplication = (MyApplication) this.getApplication();
//上記の変数からMyApplicationクラスのメンバ変数を呼び出す
textView.setText(myApplication.test);
4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?