У меня есть 2 фрагмента:
HomeFragment
, который расширяется Fragment
SettingsFragment
, который расширяется PreferenceFragment
ВMainActivity
, у меня есть 2 кнопки:
- switchButton , позволяющий переключаться между двумя фрагментами
- updateButton , позволяющий обновить строкув
SettingsFragment
(обозначается summary ) summary in SettingsFragment
- это текст TextView
внутри CustomPreference
, используемый в SettingsFragment
.
Я хочу обновить сводку внутри CustomPreference
с помощью кнопки updateButton .При первом добавлении SettingsFragment
к действию updateButton не может установить сводку .Однако, когда я использую switchButton , чтобы вернуть SettingsFragment
, он работает нормально.
Я поместил логи в начале каждой функции, надеясь отследить проблему.
Первыйвремя SettingsFragment
ставится и updateButton нажимается, я получаю следующий журнал
D/MainActivity: onCreate
D/MainActivity: Fragments button clicked
D/SettingsFragment: onCreate
D/CustomPreference: CustomPreference constructor
D/SettingsFragment: onViewCreated
D/CustomPreference: set summary : Started
D/CustomPreference: onBindView
setting mSummaryTextView to Started
D/CustomPreference: onBindView
setting mSummaryTextView to Started
D/CustomPreference: onBindView
setting mSummaryTextView to Started
D/MainActivity: Update button clicked
D/SettingsFragment: setSummary : Hello Preference (0)
D/CustomPreference: set summary : Hello Preference (0)
setting mSummaryTextView to Hello Preference (0)
Теперь, если я положу обратно SettingsFragment
во второй раз и нажму updateButton , я получаю следующий журнал
D/MainActivity: Fragments button clicked
D/SettingsFragment: onCreate
D/CustomPreference: CustomPreference constructor
D/SettingsFragment: onViewCreated
D/CustomPreference: set summary : Started
D/CustomPreference: onBindView
setting mSummaryTextView to Started
D/CustomPreference: onBindView
setting mSummaryTextView to Started
D/CustomPreference: onBindView
setting mSummaryTextView to Started
D/CustomPreference: onBindView
setting mSummaryTextView to Started
D/MainActivity: Update button clicked
D/SettingsFragment: setSummary : Hello Preference (1)
D/CustomPreference: set summary : Hello Preference (1)
setting mSummaryTextView to Hello Preference (1)
(обратите внимание, что onBindView
вызывается еще раз для второго случая)
Ниже я ставлю свой код
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
HomeFragment homeFragment = new HomeFragment();
SettingsFragment settingsFragment = new SettingsFragment();
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set settingsFragment as the default fragment
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, homeFragment).commit();
// A simple button to switch between the two fragments
Button switchButton = findViewById(R.id.switchButton);
switchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Fragments button clicked");
if(homeFragment.isAdded()) {
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, settingsFragment).commit();
} else {
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, homeFragment).commit();
}
}
});
// A simple button to change the summary of the preference
Button updateButton = findViewById(R.id.updateButton);
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Update button clicked");
if(settingsFragment.isAdded()) {
settingsFragment.setSummary("Hello Preference (" + counter + ")");
counter++;
}
}
});
}
}
HomeFragment.java
public class HomeFragment extends Fragment {
private static final String TAG = "HomeFragment";
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView textView = view.findViewById(R.id.homeText);
if(textView != null) {
textView.setText("Home fragment text (modified)");
}
}
}
SettingsFragment.java
public class SettingsFragment extends PreferenceFragment {
private static final String TAG = "SettingsFragment";
CustomPreference customPreference;
String mSummary;
public SettingsFragment() {
// Required empty public constructor
}
public void setSummary(String text) {
Log.d(TAG, "setSummary : " + text);
((CustomPreference) findPreference("test_key")).setSummary(text);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_settings);
mSummary = "Started";
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Log.d(TAG, "onViewCreated");
super.onViewCreated(view, savedInstanceState);
customPreference = (CustomPreference) findPreference("test_key");
customPreference.setSummary(mSummary);
}
}
CustomPreference.java
public class CustomPreference extends Preference {
private static final String TAG = "CustomPreference";
private String mSummary;
private TextView mSummaryTextView;
public CustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
Log.d(TAG, "CustomPreference constructor");
// Handle customized attributes
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.CustomPreference, 0, 0);
mSummary = a.getString(R.styleable.CustomPreference_summary);
// TypedArray objects are shared and must be recycled
a.recycle();
}
public void setSummary(String summary) {
Log.d(TAG, "set summary : " + summary);
this.mSummary = summary;
if (mSummaryTextView != null) {
Log.d(TAG, "setting mSummaryTextView to " + mSummary);
mSummaryTextView.setText(mSummary);
mSummaryTextView.invalidate();
}
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
Log.d(TAG, "onBindView");
mSummaryTextView = view.findViewById(R.id.summaryTextView);
if (mSummaryTextView != null) {
Log.d(TAG, "setting mSummaryTextView to " + mSummary);
mSummaryTextView.setText(mSummary);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/activityText"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center_horizontal"
android:text="MainActivity" />
<Button
android:id="@+id/updateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/activityText"
android:text="Update"/>
<Button
android:id="@+id/switchButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/updateButton"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/activityText"
android:text="Change fragment"/>
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/switchButton"
app:layout_constraintBottom_toBottomOf="parent">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
frag_home.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".HomeFragment">
<TextView
android:id="@+id/homeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="Home fragment text" />
</android.support.constraint.ConstraintLayout>
frag_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:preference="http://schemas.android.com/apk/res-auto">
<com.gosense.myapplication.CustomPreference
android:key="test_key"
preference:summary="My custom preference"
android:layout="@layout/preference_layout"/>
</PreferenceScreen>
preference_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textAllCaps="true"
android:textColor="@color/colorAccent"
android:text="Title"/>
<TextView
android:id="@+id/summaryTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/titleTextView"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textAllCaps="true" />
</android.support.constraint.ConstraintLayout>