Вторая часть серии статей про взаимодействие фрагментов и активности.
Все статьи цикла «Взаимодействие между фрагментами и активностью в Android Studio»:
- Часть 1. Подготовка
- Часть 2. Простые способы
- Часть 3. Через интерфейсы
- Часть 4. Через интенты
- Часть 5. Несколько фрагментов
Содержание
- При старте приложения отправляем информацию в фрагмент из активности
- Получаем информацию из активности в фрагменте при клике кнопки активности
- Получаем информацию из активности в фрагменте при клике кнопки фрагмента
- Получаем информацию из фрагмента в активности при клике кнопки активности
- Получаем информацию из фрагмента в активности при клике кнопки фрагмента
Предыдущая часть Часть 1. Подготовка.
При старте приложения отправляем информацию в фрагмент из активности
Задача. При запуске приложения главная активность отправляет текст Hello, World! в фрагмент, который отображается в в поле textViewFragment1 фрагмента.
Решение. При создании фрагмента нужные данные закинем в Bundle, например, по ключу text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); if (fragment == null) { fragment = new Fragment1(); Bundle bundle = new Bundle(); bundle.putString("text", "Hello, World!"); fragment.setArguments(bundle); fm.beginTransaction() .add(R.id.fragmentContainer, fragment) .commit(); } |
В фрагменте в методе onCreateView() считываем пришедшую информацию по заданному ключу text.
1 2 |
String textFromActivity = getArguments().getString("text"); textViewFragment1.setText(textFromActivity); |
Но предложенное решение не самое хорошее. Почему? Активность хранит у себя информацию о ключе фрагмента. Это не хорошо. Было бы хорошо, если бы только фрагмент хранил информацию о ключе.
В фрагменте создадим статическую переменную KEY.
1 |
public static final String KEY = "text"; |
Теперь в активности значение ключа будем вытаскивать из фрагмента Fragment1.KEY.
1 |
bundle.putString(Fragment1.KEY, "Hello, World!"); |
Приведу полные коды файлов.
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.example.workwithfragments; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private EditText editText; private TextView textView; private Button button; private FrameLayout fragmentContainer; private FrameLayout fragmentContainer2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)findViewById(R.id.editText); textView = (TextView)findViewById(R.id.textView); button = (Button)findViewById(R.id.button); fragmentContainer = (FrameLayout)findViewById(R.id.fragmentContainer); fragmentContainer2 = (FrameLayout)findViewById(R.id.fragmentContainer2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); if (fragment == null) { fragment = new Fragment1(); Bundle bundle = new Bundle(); bundle.putString(Fragment1.KEY, "Hello, World!"); fragment.setArguments(bundle); fm.beginTransaction() .add(R.id.fragmentContainer, fragment) .commit(); } } } |
Fragment1.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.example.workwithfragments; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Fragment1 extends Fragment { private EditText editTextFragment1; private TextView textViewFragment1; private Button buttonFragment1; public static final String KEY = "text"; public Fragment1() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_fragment1, container, false); editTextFragment1 = (EditText) view.findViewById(R.id.editTextFragment1); textViewFragment1 = (TextView) view.findViewById(R.id.textViewFragment1); buttonFragment1 = (Button) view.findViewById(R.id.buttonFragment1); buttonFragment1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); String textFromActivity = getArguments().getString("text"); textViewFragment1.setText(textFromActivity); return view; } } |
Получаем информацию из активности в фрагменте при клике кнопки активности
Задача. При нажатии на кнопку button в главной активности текст из editText главной активности должен отобразится в поле textViewFragment1 фрагмента.
Решение. Тут всё просто. Реализуем какой-нибудь метод во фрагменте, который будем вызвать, чтобы отправить во фрагмент информацию. Например, реализуем метод sendData().
1 2 3 4 |
public void sendData(String data) { if(data != null) textViewFragment1.setText(data); } |
Теперь в клике кнопки button активности нам нужно найти текущий фрагмент, который находится в контейнере fragmentContainer.
1 2 |
Fragment1 fragment1 = (Fragment1)getSupportFragmentManager() .findFragmentById(R.id.fragmentContainer); |
А потом уже у фрагмента вызвать наш метод sendData().
1 2 3 4 5 6 7 8 9 10 11 12 |
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment1 fragment1 = (Fragment1)getSupportFragmentManager() .findFragmentById(R.id.fragmentContainer); if (fragment1 != null) { String S = editText.getText().toString(); fragment1.sendData(S); } } }); |
Полный код MainActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.example.workwithfragments; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private EditText editText; private TextView textView; private Button button; private FrameLayout fragmentContainer; private FrameLayout fragmentContainer2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)findViewById(R.id.editText); textView = (TextView)findViewById(R.id.textView); button = (Button)findViewById(R.id.button); fragmentContainer = (FrameLayout)findViewById(R.id.fragmentContainer); fragmentContainer2 = (FrameLayout)findViewById(R.id.fragmentContainer2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment1 fragment1 = (Fragment1)getSupportFragmentManager() .findFragmentById(R.id.fragmentContainer); if (fragment1 != null) { String S = editText.getText().toString(); fragment1.sendData(S); } } }); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); if (fragment == null) { fragment = new Fragment1(); Bundle bundle = new Bundle(); bundle.putString(Fragment1.KEY, "Hello, World!"); fragment.setArguments(bundle); fm.beginTransaction() .add(R.id.fragmentContainer, fragment) .commit(); } } } |
Полный код Fragment1.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.example.workwithfragments; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Fragment1 extends Fragment{ private EditText editTextFragment1; private TextView textViewFragment1; private Button buttonFragment1; public static final String KEY = "text"; public Fragment1() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_fragment1, container, false); editTextFragment1 = (EditText) view.findViewById(R.id.editTextFragment1); textViewFragment1 = (TextView) view.findViewById(R.id.textViewFragment1); buttonFragment1 = (Button) view.findViewById(R.id.buttonFragment1); buttonFragment1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); String textFromActivity = getArguments().getString("text"); textViewFragment1.setText(textFromActivity); return view; } public void sendData(String data) { if(data != null) textViewFragment1.setText(data); } } |
Получаем информацию из активности в фрагменте при клике кнопки фрагмента
Задача. При нажатии на кнопку buttonFragment1 в фрагменте текст из editText главной активности должен отобразится в поле textViewFragment1 фрагмента.
Если в прошлый раз мы нажимали кнопку в активности, то сейчас будем нажимать в фрагменте.
В коде я откатываюсь к точке сохранения 1.
Решение. Мы из фрагмента можем получить доступ к активности, где располагается данный фрагмент, через getActivity(). Поэтому код клика кнопки buttonFragment1 будет таким.
1 2 3 4 5 6 7 8 |
buttonFragment1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText editText = (EditText) getActivity().findViewById(R.id.editText); String S = editText.getText().toString(); textViewFragment1.setText(S); } }); |
Полный код MainActivity.java. По сравнению с точкой сохранения тут ничего не поменялось.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.example.workwithfragments; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private EditText editText; private TextView textView; private Button button; private FrameLayout fragmentContainer; private FrameLayout fragmentContainer2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)findViewById(R.id.editText); textView = (TextView)findViewById(R.id.textView); button = (Button)findViewById(R.id.button); fragmentContainer = (FrameLayout)findViewById(R.id.fragmentContainer); fragmentContainer2 = (FrameLayout)findViewById(R.id.fragmentContainer2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); if (fragment == null) { fragment = new Fragment1(); fm.beginTransaction() .add(R.id.fragmentContainer, fragment) .commit(); } } } |
Полный код Fragment1.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.example.workwithfragments; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Fragment1 extends Fragment { private EditText editTextFragment1; private TextView textViewFragment1; private Button buttonFragment1; public Fragment1() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_fragment1, container, false); editTextFragment1 = (EditText) view.findViewById(R.id.editTextFragment1); textViewFragment1 = (TextView) view.findViewById(R.id.textViewFragment1); buttonFragment1 = (Button) view.findViewById(R.id.buttonFragment1); buttonFragment1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText editText = (EditText) getActivity().findViewById(R.id.editText); String S = editText.getText().toString(); textViewFragment1.setText(S); } }); return view; } } |
Получаем информацию из фрагмента в активности при клике кнопки активности
Задача. При нажатии на кнопку button в активности текст из editTextFragment1 фрагмента должен отобразится в поле textView активности.
В коде я откатываюсь к точке сохранения 1.
Решение. Мы можем в фрагменте определить метод getData(), который будет возвращать значение из поля ввода.
1 2 3 |
String getData() { return editTextFragment1.getText().toString(); } |
Теперь в активности мы можем при клике кнопки вызвать этот метод getData().
1 2 3 4 5 6 7 8 9 10 11 12 |
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment1 fragment1 = (Fragment1)getSupportFragmentManager() .findFragmentById(R.id.fragmentContainer); if (fragment1 != null) { String S = fragment1.getData(); textView.setText(S); } } }); |
Файл MainActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.example.workwithfragments; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private EditText editText; private TextView textView; private Button button; private FrameLayout fragmentContainer; private FrameLayout fragmentContainer2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)findViewById(R.id.editText); textView = (TextView)findViewById(R.id.textView); button = (Button)findViewById(R.id.button); fragmentContainer = (FrameLayout)findViewById(R.id.fragmentContainer); fragmentContainer2 = (FrameLayout)findViewById(R.id.fragmentContainer2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment1 fragment1 = (Fragment1)getSupportFragmentManager() .findFragmentById(R.id.fragmentContainer); if (fragment1 != null) { String S = fragment1.getData(); textView.setText(S); } } }); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); if (fragment == null) { fragment = new Fragment1(); fm.beginTransaction() .add(R.id.fragmentContainer, fragment) .commit(); } } } |
Файл Fragment1.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package com.example.workwithfragments; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Fragment1 extends Fragment { private EditText editTextFragment1; private TextView textViewFragment1; private Button buttonFragment1; String getData() { return editTextFragment1.getText().toString(); } public Fragment1() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_fragment1, container, false); editTextFragment1 = (EditText) view.findViewById(R.id.editTextFragment1); textViewFragment1 = (TextView) view.findViewById(R.id.textViewFragment1); buttonFragment1 = (Button) view.findViewById(R.id.buttonFragment1); buttonFragment1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); return view; } } |
Получаем информацию из фрагмента в активности при клике кнопки фрагмента
Задача. При нажатии на кнопку buttonFragment1 в фрагменте текст из editTextFragment1 фрагмента должен отобразится в поле textView активности.
В коде я откатываюсь к точке сохранения 1.
Решение. Мы из фрагмента можем получить доступ к активности, где располагается данный фрагмент, через getActivity(). Поэтому код клика кнопки buttonFragment1 будет таким.
1 2 3 4 5 6 7 8 |
buttonFragment1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TextView textView = (TextView) getActivity().findViewById(R.id.textView); String S = editTextFragment1.getText().toString(); textView.setText(S); } }); |
Файл MainActivity.java. Он не меняется.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.example.workwithfragments; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private EditText editText; private TextView textView; private Button button; private FrameLayout fragmentContainer; private FrameLayout fragmentContainer2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)findViewById(R.id.editText); textView = (TextView)findViewById(R.id.textView); button = (Button)findViewById(R.id.button); fragmentContainer = (FrameLayout)findViewById(R.id.fragmentContainer); fragmentContainer2 = (FrameLayout)findViewById(R.id.fragmentContainer2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); if (fragment == null) { fragment = new Fragment1(); fm.beginTransaction() .add(R.id.fragmentContainer, fragment) .commit(); } } } |
Файл Fragment1.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.example.workwithfragments; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Fragment1 extends Fragment { private EditText editTextFragment1; private TextView textViewFragment1; private Button buttonFragment1; public Fragment1() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_fragment1, container, false); editTextFragment1 = (EditText) view.findViewById(R.id.editTextFragment1); textViewFragment1 = (TextView) view.findViewById(R.id.textViewFragment1); buttonFragment1 = (Button) view.findViewById(R.id.buttonFragment1); buttonFragment1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TextView textView = (TextView) getActivity().findViewById(R.id.textView); String S = editTextFragment1.getText().toString(); textView.setText(S); } }); return view; } } |
Следующая часть Часть 3. Через интерфейсы.