Tokyo GTUG Bootcamp2010
-
Upload
takashi-egawa -
Category
Technology
-
view
2.692 -
download
0
description
Transcript of Tokyo GTUG Bootcamp2010
Tokyo GTUG主催 Bootcamp 2010Android:
Activity間の情報の受け渡し方
江川 崇(@t_egg/ egg)
自己紹介• 江川 崇(EGAWA Takashi)
• プログラマー• egg / @t_egg
• Android Developers’ Club(デ部)
• Android プログラミング入門
• IMoNiなど
今日お話ししたいこと
「Activity間の情報の受け渡し方」
今日お話ししたいこと
「Activity間の情報の受け渡し方」
たくさんありますが、今回はその中でも、なるべく簡単にできる方法をご紹介します。
私も知らないことがたくさんありますし、皆さんと一緒に理解を深めていきたいので、随時質問して下さい。
startActivity
public class A extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
}
public void click(View v) {
Intent i = new Intent(this, B.class);
startActivity(i);
}
}
startActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/andr
oid"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button"
android:onClick="click"
/>
</LinearLayout>
layout/a.xml
Can I ask?
•ボタンが押されたときに、clickメソッドが呼ばれます。紐付けはどこで?
•startActivityが呼ばれると何が起きますか?
Can I ask?
•ボタンが押されたときに、clickメソッドが呼ばれます。紐付けはどこで?
android:onClick
•startActivityが呼ばれると何が起きますか?
指定されたActivityが最前面に来る※ Activityを選択するDialogが出るか、Activityが
見つからずにエラーになるケースもある
startActivity
public class A extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
}
public void click(View v) {
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com/"));
startActivity(i);
}
}
こう変えたらどうなる?
Intent
• An intent is an abstract description of an operation to be performed.
• 「Intentとは実行される操作の抽象的な単位です。」▫ 一つのアクティビティ内のウィジェットやイベントは単
一のスレッドで実行される
▫ Handlerドリブン、Looperでのループ
▫ でも、そうとは限らないので、画面遷移をはじめとするあらゆるコンポーネント間連携を隠ぺいする(開発者が楽になり、繋がりやすくなる)
ComponentName
• private ComponentName mComponent;▫ インテントの宛先のこと
▫ 明示的なインテント(Explicit Intents) Intent(Context packageContext, Class cls)や、
setComponentメソッドなどによって、そのインテントを取り扱うコンポーネントを「明示的」に指定されたもの
宛先を特定する情報は他には要らない
▫ 暗黙的なインテント(Implicit Intents) 宛先のコンポーネントを指定されていないもの そのIntentを動かすにあたってどのコンポーネントが最適であ
るかを決定するための十分な情報が必要
Can I ask?
•呼び出したActivityから戻るときは?
•呼び出したActivityから結果を受け取るときは?
Can I ask?
•呼び出したActivityから戻るときは?finish
•呼び出したActivityから結果を受け取るときは?
startActivityForResultsetResult
onActivityResult
startActivityForResult
public class A extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
}
public void click(View v) {
Intent i = new Intent(this, B.class);
startActivityForResult(i, 0);
}
@Override
protected void onActivityResult(int requestCode, int
resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
・・・}
}
startActivityForResult
public class B extends Activity {
・・・@Override
protected void onPause() {
super.onPause();
setResult(10);
finish();
}
}
あくまでも例ですが。
Can I ask?
•複数画面にまたがるときはどうやって受け渡す?(such as A -> B -> C)
Can I ask?
•複数画面にまたがるときはどうやって受け渡す?(such as A -> B -> C)
startActivityResultの数珠つなぎほか、色々.
いくつかご紹介します
static
•staticな入れ物(クラス変数)に入れておけばいい
public class A extends Activity {
static int val;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
}
public void click(View v) {
System.out.println(++val);
}
}
Can I ask?
•気を付けるべきことは?
Can I ask?
•気を付けるべきことは?アプリの終了ではなくプロセスが死ぬまで覚えている
同期化されない
など、他にも実はいろいろです。
android.app.Application
•Activity ≒ 画面 (短命)•Appication ≒ アプリ (長寿)
public class MyApplication extends Application {
public String val;
}
android.app.Application•AndroidManifest.xml
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:name=".MyApplication">
<activity android:name=".A"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".B"
android:label="@string/app_name">
</activity>
</application>
Can I ask?
•気を付けるべきことは?
Can I ask?
•気を付けるべきことは?アプリを終了したら忘れる
別プロセスは別アプリインスタンス
など
ファイルに保存
•abstract FileInputStream openFileInput(String name)Open a private file associated with this Context's application package for reading.
•abstract FileOutputStream openFileOutput(String name, int mode)Open a private file associated with this Context's application package for writing.
•android.context.Context
•普通にjava.ioでもできます。(アクセスできる場所なら)
キャッシュ•android.context.Context#
getCacheDir()•public abstract File getCacheDir ()Returns the absolute path to the application specific cache directory on the filesystem.
Can I ask?
•気を付けるべきことは?
Can I ask?
•気を付けるべきことは?キャッシュは消えるかも同期化などデカくし過ぎると迷惑アンインストールしたら忘れる
など
SharedPreferences
Context context = getApplicationContext();
SharedPreferences pref = (SharedPreferences)
PreferenceManager.getDefaultSharedPreferences(context);
// 読む場合String val1 = pref.getString("KEY_1", "");
int val2 = pref.getInt("KEY_2", -1);
// 書く場合pref.edit().putBoolean("KEY_3", true).commit();
•KEY,VALUE•アプリごとのデータ領域に置かれる
Can I ask?
•気を付けるべきことは?
Can I ask?
•気を付けるべきことは?デカくなると遅くなる(XML)複数プロセスからのアクセスは同期化されない
Note: currently this class does not support use across multiple processes. This will be added later.
など
【参考】他のアプリのContextを取る
•foo.barアプリの、あるアクティビティで保存
getSharedPreferences("HOGEHOGE", MODE_WORLD_READABLE |
MODE_WORLD_WRITEABLE).edit().putInt("value", 10).commit();
Context otherAppContext = null;
try {
otherAppContext = createPackageContext("foo.bar",
Context.MODE_WORLD_READABLE);
} catch (NameNotFoundException e) {
}
Context context = otherAppContext.getSharedPreferences("HOGEGOGE", 0);
•別のアプリから、foo.barアプリのContextを取る
その他の方法(一例)•Serviceで管理する
•別アプリに覚えさせておく
•端末の外に覚えさせておく•DropboxのようなストレージでもいいしApp Engineなどでもいい•同期例:Sample Sync Adapter(development/samples/SampleSyncAdapter/)
•SQLiteに記録する
•ContentProviderに置く•独自で公開してもよいし、ギャラリーの画像のExifデータに置く( #uso800