Answer the question
In order to leave comments, you need to log in
Why is the same text displayed on a TextView with the same id in different layouts?
There is a fragment code like this:
public class FragmentSettings extends Fragment {
@Bind(R.id.settings_layout) LinearLayout settingsLayout;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = (ViewGroup) inflater.inflate(R.layout.fragment_settings, null);
ButterKnife.bind(this, v);
settingsLayout.addView(IconTextView.create(
getResources().getDrawable(R.drawable.settings_profile),
"Profile data",
getActivity(),
new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),
ProfileSettingActivity.class));
}
}
));
settingsLayout.addView(IconTextView.create(
getResources().getDrawable(R.drawable.settings_notifications),
"Notifications",
getActivity(),
new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),
NotificationSettingActivity.class));
}
}
));
return v;
}
....
public class IconTextView extends FrameLayout{
private static int res = R.layout.settings_element;
public IconTextView(Context context) {
super(context);
}
public static IconTextView create(Drawable icon, String text, Context context, OnClickListener listener) {
IconTextView view = new IconTextView(context);
View content = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(res, view, false);
((ImageView) content.findViewById(R.id.icon)).setImageDrawable(icon);
((TextView) content.findViewById(R.id.setting_title)).setText(text);
view.addView(content);
view.setOnClickListener(listener);
return view;
}
}
Answer the question
In order to leave comments, you need to log in
When leaving and returning to another Activity, the fragment must call on(Save/Restore)InstanseState(). Most likely, these methods are not overridden for you, so the system itself saves the state of your TextView, and then restores it itself. Since they have the same id (and saving occurs by id), each next text overwrites the previous one and, when restored from, is written to all TextView
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question