简体   繁体   中英

Fragment “overwrites” another fragment when first selected

I have Bottom Navigation with which I switch between 3 fragments: "ConnectFragment", "DashboardFragment" and "ChatFragment".

Switching from Connect to Chat and vice versa works OK, but when I select Dashboard it causes a bug that makes Dashboard appear when selecting Chat in navigation, what is causing this?

All 3 fragments have identical functionality and layout, so i assume the problem lays in MainActivity.

MainActivity:

public class MainActivity extends AppCompatActivity implements DashboardFragment.FragmentDashListener, ChatFragment.FragmentChatListener, ConnectFragment.FragmentConnListener {

    FragmentManager fm = getSupportFragmentManager();
    Fragment active;

    Fragment FragmentConnect = new ConnectFragment();
    Fragment FragmentDashboard = new DashboardFragment();
    Fragment FragmentChat = new ChatFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);


        fm.beginTransaction().add(R.id.fragment_container, FragmentChat).hide(FragmentChat).commit();        //Ustvari vse 3 fragmente, skrije 2 da se nena vedno znova kreirajo
        fm.beginTransaction().add(R.id.fragment_container, FragmentDashboard).hide(FragmentDashboard).commit();
        fm.beginTransaction().add(R.id.fragment_container, FragmentConnect).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            active = FragmentConnect;

            switch (menuItem.getItemId()) {
                case R.id.nav_connect:
                    fm.beginTransaction().hide(active).show(FragmentConnect).commit();
                    active = FragmentConnect;
                    return true;

                case R.id.nav_dashboard:
                    fm.beginTransaction().hide(active).show(FragmentDashboard).commit();
                    active = FragmentDashboard;
                    return true;

                case R.id.nav_send:
                    fm.beginTransaction().hide(active).show(FragmentChat).commit();
                    active = FragmentChat;
                    return true;
            }
            return false;
        }
    };

    @Override
    public void onInputChatSent(CharSequence input) {
        ConnectFragment.updateEditText(input);
    }

    @Override
    public void onInputConnSent(CharSequence input) {
        DashboardFragment.updateEditText(input);
    }

    @Override
    public void onInputDashSent(CharSequence input) {
        ChatFragment.updateEditText(input);
    }

}

Fragments:

All 3 fragments have identical code, below are Dashboard and Chat.

public class DashboardFragment extends Fragment {

    private FragmentDashListener listener;
    private static EditText editText;
    private Button ButtonOk;

    public interface FragmentDashListener{
        void onInputDashSent (CharSequence input);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_dashboard, container, false);
        editText = v.findViewById(R.id.edit_text);
        ButtonOk = v.findViewById(R.id.Button_Ok);
        ButtonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CharSequence input = editText.getText();
                listener.onInputDashSent(input);
            }
        });
        return v;
    }

    public static void updateEditText(CharSequence newText){
        editText.setText(newText);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if(context instanceof FragmentDashListener){
            listener = (FragmentDashListener) context;
        }   else {
            throw new RuntimeException(context.toString()+"must implement FragmentDashListener");
        }
    }
}
public class ChatFragment extends Fragment {

    private FragmentChatListener listener;
    private static EditText editText;
    private Button ButtonOk;

    public interface FragmentChatListener{
        void onInputChatSent (CharSequence input);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_chat, container, false);
        editText = v.findViewById(R.id.edit_text);
        ButtonOk = v.findViewById(R.id.Button_Ok);
        ButtonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CharSequence input = editText.getText();
                listener.onInputChatSent(input);
            }
        });
        return v;
    }

    public static void updateEditText(CharSequence newText){
        editText.setText(newText);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if(context instanceof FragmentChatListener){
            listener = (FragmentChatListener) context;
        }   else {
            throw new RuntimeException(context.toString()+"must implement FragmentChatListener");
        }
    }
}

Remove the first line

active = FragmentConnect;

from onNavigationItemSelected method. This will fix the issue

or modify it as below

if(active == null) {
    active = FragmentConnect;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM