Kenny
Kenny

Reputation: 256

popup-AlertDialog wrongly appear in other fragment

My java Project have 4 fragment.

One of the fragment is a chat room, it will require user to input name in a popup-alertDialog when page is called.

Here is the bug. When I put the chatroom to Tab 4, Tab 3 will wrongly display the alertDialog when it(Tab3) was clicked.

When I put the chatroom to Tab 2, Tab 1 and Tab 3 will also have the same problem.

It is tricky to me, I have no idea where the bug come from.

Here is my chatroom.java

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.tab_match_chatroom, container, false);
    add_room = (Button) v.findViewById(R.id.btn_add_room);
    room_name = (EditText) v.findViewById(R.id.room_name_edittext);
    listView = (ListView) v.findViewById(R.id.listView);
    listView.setAdapter(arrayAdapter);

    add_room.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put(room_name.getText().toString(), "");
            root.updateChildren(map);
        }
    });
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            Intent intent = new Intent(getActivity().getApplicationContext(), TabInChatRoom.class);
            intent.putExtra("room_name", ((TextView) view).getText().toString());
            intent.putExtra("user_name", name);
            startActivity(intent);
        }
    });
    return v;
}

public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list_of_rooms);
    request_user_name();

    root.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Set<String> set = new HashSet<String>();
            Iterator i = dataSnapshot.getChildren().iterator();
            while (i.hasNext()) {
                set.add(((DataSnapshot) i.next()).getKey());
            }
            list_of_rooms.clear();
            list_of_rooms.addAll(set);
            arrayAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

private void request_user_name() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Enter name:");

    final EditText input_field = new EditText(getActivity());

    builder.setView(input_field);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            name = input_field.getText().toString();
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.cancel();
            request_user_name();
        }
    });

    builder.show();
}

Upvotes: 0

Views: 26

Answers (1)

You are showing the popup when the 4th fragment is created (inside onCreate method)

How is it working?

There are multiple related things triggering the issue.

1- ViewPager has offset page limit and default value is 1 (3 Tabs at once). You can change this value. That means, your viewPager only creates 1 from left side, 1 from right side of your current page. Others will be destroyed and recreated when you select fragment in the range limit.

2- When ViewPager has more fragment than is offsetPageLimit, it will not create further ranged Fragments For example: If you have 4 Fragments, first fragment and the 2nd one will be created at start, 4th one will be created when you selected 3rd or 4th tabs.

3- When you select the 3rd or 4th tabs, you are recreating the 4th fragment and it calls onCreate method and afterwards it calls the request_user_name() eventually. And your popup shows up.

First, you can change the viewPagers offsetPage limit by 4 for convenience.

viewPager.setOffscreenPageLimit(4);

Secondly, you need to call request_user_name() method when the Tab 4 clicked, not the 4th fragment created.

You can use OnPageChangeListener like below:

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if(position == 3){
                //that means 4th tab
                //show the popup
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

More Info:

Link 1

Link 2

Upvotes: 1

Related Questions