如何使用BottomNavigationView保存片段实例?

时间:2018-06-22 18:25:23

标签: java android android-fragments

我有3个片段A,B和C,其中A上有一个计数器。当我从A切换时,即A-> B || A-> C,然后我切换回A,尽管我将实例保存在片段中,但所有值都消失了:

public class ItemOneFragment extends Fragment implements OnClickListener{


  public static ItemOneFragment newInstance() {
        ItemOneFragment fragment = new ItemOneFragment();
        return fragment;
    }
    Button add1;
    Button add2;
    Button add3;
    Button min1;
    Button min2;
    Button min3;
    Button clear;
    Spinner numbers;
    TextView scoreText1;
    TextView scoreText2;
    TextView scoreText3;
    int counter1 = 0;
    int counter2 = 0;
    int counter3 = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {counter1 = 0; counter2 = 0; counter3 = 0;}
    else {
        counter1 = savedInstanceState.getInt("counter1",0);
        counter2 = savedInstanceState.getInt("counter2",0);
        counter3 = savedInstanceState.getInt("counter3",0);}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    return  inflater.inflate(R.layout.fragment_item_one, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);

    numbers = (Spinner) getActivity().findViewById(R.id.spinner);
    add1 = (Button) getActivity().findViewById(R.id.button12);
    add2 = (Button) getActivity().findViewById(R.id.button13);
    add3 = (Button) getActivity().findViewById(R.id.button15);
    min1 = (Button) getActivity().findViewById(R.id.button2);
    min2 = (Button) getActivity().findViewById(R.id.button14);
    min3 = (Button) getActivity().findViewById(R.id.button16);
    clear = (Button) getActivity().findViewById(R.id.button11);
    scoreText1 = (TextView) getActivity().findViewById(R.id.textView_return1);
    scoreText2 = (TextView) getActivity().findViewById(R.id.textView_return2);
    scoreText3 = (TextView) getActivity().findViewById(R.id.textView_return3);
    resultText = (TextView) getActivity().findViewById(R.id.textView10);

    String[] arraySpinner = new String[]{
            "1", "2", "3", "4", "5", "6", "7", "8", "9", "10","11","12"
    };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, arraySpinner);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    numbers.setAdapter(adapter);
    add1.setOnClickListener(this);
    add2.setOnClickListener(this);
    add3.setOnClickListener(this);
    min1.setOnClickListener(this);
    min2.setOnClickListener(this);
    min3.setOnClickListener(this);
    clear.setOnClickListener(this);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    //Save the fragment's state here


    outState.putInt("counter1",counter1);
    outState.putInt("counter2",counter2);
    outState.putInt("counter3",counter3);
    super.onSaveInstanceState(outState);
}


@Override
public void onClick(View v) {

    if (v == add1) {
        counter1++;
        scoreText1.setText(Integer.toString(counter1));
    }
    if (v == add2) {
        counter2++;
        scoreText2.setText(Integer.toString(counter2));
    }
    if (v == add3) {
        counter3++;
        scoreText3.setText(Integer.toString(counter3));
    }
    if (v == min1 && counter1 != 0) {
        counter1--;
        scoreText1.setText(Integer.toString(counter1));
    }
    if (v == min2 && counter2 != 0) {
        counter2--;
        scoreText2.setText(Integer.toString(counter2));
    }
    if (v == min3 && counter3 != 0) {
        counter3--;
        scoreText3.setText(Integer.toString(counter3));
    }
    if (v == clear) {
        counter1 = 0;
        counter2 = 0;
        counter3 = 0;
        scoreText1.setText(Integer.toString(counter1));
        scoreText2.setText(Integer.toString(counter2));
        scoreText3.setText(Integer.toString(counter3));
    }
    int s = Integer.parseInt(numbers.getSelectedItem().toString());


}
}

经过一些研究,我相信我的片段实例未正确保存在MainActivity中,并且每次在底部导航器中进行选择时都会调用一个新实例:

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BottomNavigationView bottomNavigationView = (BottomNavigationView)
            findViewById(R.id.navigation);

    bottomNavigationView.setOnNavigationItemSelectedListener
            (new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;
                    switch (item.getItemId()) {
                        case R.id.calculation:
                            selectedFragment = ItemOneFragment.newInstance();
                        break;
                        case R.id.info:
                            selectedFragment = ItemTwoFragment.newInstance();
                            break;
                        case R.id.disclaimer:
                            selectedFragment = ItemThreeFragment.newInstance();
                            break;
                    }
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame_layout, selectedFragment);


                    transaction.commit();
                    return true;
                }
            });

    //Manually displaying the first fragment on start up
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_layout, ItemOneFragment.newInstance(),"Fragment1");
    transaction.commit();


}

我是应用程序编码的新手,非常感谢任何能使我了解如何使用底部导航器保存片段的人。

0 个答案:

没有答案