0

错误描述

我是使用 Fragment 的新手。如上图所示,在某些情况下按下Back键时会出现画面重叠的问题。有时使用replace代替addToBackstack,尝试将onBackPressed函数放入activity进行控制,分片失败。我想知道代码有什么问题。我在这个问题上坚持了几天:(

主要活动

public class MainActivity extends FragmentActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
final String TAG = "MainActivity";
BottomNavigationView bottomNavigationView;
public Fragment fragmentA,fragmentB;
public Fragment fragmentB_1;

public static final int B_1_FRAGMENT = 1003;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i(TAG, "onCreate");

    if (findViewById(R.id.fragment_container) != null) {
        if (savedInstanceState != null) {
            return;
        }
        fragmentInit();
    }
    bottomNavigationInit();
}

private void fragmentInit() {
    fragmentA = new FragmentA();
    fragmentB = new FragmentB();
    
    fragmentB_1 = new FragmentB_1();

    fragmentA.setArguments(getIntent().getExtras());
    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fragmentA, "a").commit();
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_a) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragmentA, "a")
                .commit();
    } else if (id == R.id.action_b) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragmentB, "b")
                .commit();
    } else {
        Log.i(TAG, "onNavigationItemSelected else");
    }

    return true;
}

private void bottomNavigationInit() {
    bottomNavigationView = findViewById(R.id.bottomNavigationView);
    bottomNavigationView.setOnNavigationItemSelectedListener(this);
}

public void changeFragment(int index) {
    switch (index) {
        
        case B_1_FRAGMENT:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragmentB_1)
                    .addToBackStack("b_1").commit();
            break;
        /*
        *  ... 
        * 
        * */
    }
}}

片段 A

...Nothing Special

片段 B

public class FragmentB_1 extends Fragment implements View.OnClickListener{
MainActivity activity;
View view;
CardView cvFramgentB1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_calculator, container, false);
    btnInit();
    return view;
}
private void btnInit() {
    cvFramgentB1 = view.findViewById(R.id.cv_fragmentB_1);
}
@Override
public void onStart() {
    super.onStart();
    cvFramgentB1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.cv_proficiency) {
        activity.changeFragment(B_1_FRAGMENT);
    }
}
@Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);
    activity = (MainActivity) getActivity();
}
@Override
public void onDetach() {
    super.onDetach();
    activity = null;
}}

片段 B_1

...Nothing Special
4

0 回答 0