Demo
구성
- activity
- layout
- fragment
MainActivity.java
package com.sjcoding.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*ExampleFragment fragment = new ExampleFragment();
Bundle args = new Bundle();
args.putString("argText", "example text");
args.putInt("argNumber", 123);
fragment.setArguments(args);*/
ExampleFragment fragment = ExampleFragment.newInstance("example text", 123);
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
}
}
ExampleFragment
package com.sjcoding.myapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class ExampleFragment extends Fragment {
// 받을 argument
private static final String ARG_TEXT = "argText";
private static final String ARG_NUMBER = "argNumber";
private String text;
private int number;
// instance create
public static ExampleFragment newInstance(String text, int number) {
ExampleFragment fragment = new ExampleFragment();
Bundle args = new Bundle();
args.putString(ARG_TEXT, text);
args.putInt(ARG_NUMBER, number);
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.example_fragment, container, false);
// text view에 글자 셋팅 (넘겨받은 argument들로)
TextView textView = v.findViewById(R.id.tv_fragment);
if (getArguments() != null) {
text = getArguments().getString(ARG_TEXT);
number = getArguments().getInt(ARG_NUMBER);
}
textView.setText(text + number);
return v;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
tools:context=".MainActivity">
</FrameLayout>
example_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_fragment"
android:text="Fragment TextView"
android:textSize="32dp"/>
</LinearLayout>
'System Programming > Android' 카테고리의 다른 글
[Android/Java] Fragment Bottom Navigation view (0) | 2021.06.28 |
---|---|
[Android/Java] Fragment간 값 전달 (0) | 2021.06.28 |
[Android/Kotlin] Live Data (0) | 2021.06.28 |
[Android/Java] naver map api (0) | 2021.06.19 |
[Android/Firebase] Firebase 연동 (0) | 2021.06.16 |