切换片段时如何维护片段状态

时间:2017-08-14 17:34:40

标签: java android android-fragments

在我的MainActivity课程中,我有一个BottomNavigationView,其中有3个标签可在活动(家庭,搜索和个人)之间切换。每当我点击任何一个标签时,它都会拉出相应的片段,但我相信我每次都会调用一个新的片段。

我需要这些片段来维护它们所做的任何更改(类似于Instagram的工作方式)。每个片段目前都是非常基本的(新创建并且未更改),但是当我切换到另一个片段时,我希望以一种保存状态的方式设置它们,并在我返回时恢复它们。

下面是我的主要活动和家庭片段的代码。

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BottomNavigationView navigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
    navigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.content, new HomeFragment()).commit();
}

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener =
        new BottomNavigationView.OnNavigationItemSelectedListener(){
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item){
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                switch (item.getItemId()){
                    case R.id.HomeItem:
                        transaction.replace(R.id.content, new HomeFragment()).commit();
                        return true;
                    case R.id.SearchItem:
                        transaction.replace(R.id.content, new SearchFragment()).commit();
                        return true;
                    case R.id.PersonalItem:
                        transaction.replace(R.id.content, new PersonalFragment()).commit();
                        return true;
                }
                return false;
            }
        };
}


public class HomeFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public HomeFragment() {
    // Required empty public constructor
}

// TODO: Rename and change types and number of parameters
public static HomeFragment newInstance(String param1, String param2) {
    HomeFragment fragment = new HomeFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_home, container, false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        Toast.makeText(context, "Home Fragment Attached", Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
}

1 个答案:

答案 0 :(得分:0)

您必须使用transaction.hide(fragment)和transaction.show(fragment)而不是transaction.replace,因为替换将删除片段并再次添加,删除其保存的状态。 按照我的方式行事将调用碎片' onPause和onReaume方法,不重置其状态。

希望它有所帮助!