P
P
Pavel Kuznetsov2015-11-20 15:54:47
Android
Pavel Kuznetsov, 2015-11-20 15:54:47

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;
    }
}

setting_element layout:
55995b4f11104d3b9b164a31b245fd86.PNG
State when first launched:
1f59c26d48d0469884b7471c29cdb967.png
State after the bug:
7a126ab6e5c74154a89f35299bdd6a42.png
If you open another Activity and then return to this fragment, ALL settings items display the text of the last item. If you remove the Test (as in the screenshot), then both items will be Notifications.
Facts:
1. At the end of the onCreateView operation, the text is correct in both cases, probably the bug occurs somewhere in the fragment.onStart or onDestroy area
2. If the title of the fragment ("Settings") is set to id "setting_title", then it also changes its text to the text of the last setting item.
3. Before that, instead of a custom view, I used include and filled in the text in the fragment method - the result is the same, the bug appeared.
4. ButterKnife is not to blame, without it the bug also appears
5. With the icons, everything is in order, although they are called in the same way

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Lebedev, 2015-11-20
@cucumber007

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

O
one pavel, 2015-11-20
@onepavel

From what is posted, I do not see a bug.
I propose to override the TextView and see who is visiting the text.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question