Android ActivityからFragmentに引数を渡す
サクッとできるように作ってみよう。
ボタンを押したらテキストが変わるだけの簡易な画面。
Activityの処理
インスタンスの生成はFragment側で行う(9行目)
DemoFragment.newInstance(getString(R.string.demo_arg_text));
Activityは引数を渡すのみにしたいので、
Bundleにputするときの「key」を知らなくてよい設計にする。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if (null == savedInstanceState) {
DemoFragment fragment = DemoFragment.newInstance(getString(R.string.demo_arg_text));
getSupportFragmentManager().beginTransaction()
.add(R.id.main_container, fragment)
.commit();
}
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</FrameLayout>
Fragmentの処理
newInstance(String msg)で
Fragmentのインスタンス生成後、Bundleにkey、valueを詰め込み
fragment.setArguments(bundle);
でセットすることで引数に渡された値を取得することができる。
onCreate()で渡された値を取得する
mMsg = getArguments().getString(MSG_KEY);
あとは、ボタンが押されたときにTextViewが変わるようにする。
ボタンのクリックイベント処理はonCreateView()で行う。
DemoFragment.java
public class DemoFragment extends Fragment {
private static final String MSG_KEY = "msg_key";
private String mMsg;
public DemoFragment() {
// 引数無しのコンストラクタは必須
// 再生成時に利用される
}
public static DemoFragment newInstance(String msg) {
DemoFragment fragment = new DemoFragment();
Bundle bundle = new Bundle();
bundle.putString(MSG_KEY, msg);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mMsg = getArguments().getString(MSG_KEY);
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.demo_fragment, container, false);
final TextView textView = view.findViewById(R.id.tv_id);
Button button = view.findViewById(R.id.btn_id);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView.setText(mMsg);
}
});
return view;
}
}
demo_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/demo_tv_text"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@id/btn_id" />
<Button
android:id="@+id/btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/demo_btn_text"
app:layout_constraintTop_toTopOf="@id/tv_id"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
values/strings.xml
<resources>
<string name="demo_tv_text">変わるかな?</string>
<string name="demo_btn_text">押すと変わる</string>
<string name="demo_arg_text">ちぇーーーんじ!</string>
</resources>
まとめ
setArguments()
で引数に渡された値を詰め込んで
getArguments()
で値を取得する
という流れがつかめればOK。

ディスカッション
コメント一覧
まだ、コメントがありません