我在我的应用程序中使用了两个片段来显示列表和详细信息视图。当我按下列表片段时,它应该显示详细视图片段,当我按下它时,它应该显示列表片段。这个流程工作正常,但问题是,它每次加载列表片段(而不是恢复它创建片段的片段)。任何身体面临的问题都可以请帮我解决这个问题。
//活动类
public class MainActivity extends ActionBarActivity {
/** Activity Override - Start */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_folio_level);
System.out.println("Inside the FolioLevelActivity onCreate() method!!!!!!!!");
Bundle bundle = getIntent().getExtras();
FirstFragment fragment = new FirstFragment ();
fragment.setArguments(bundle);
if(savedInstanceState==null){
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,fragment).commit();
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.right_to_left_in, R.anim.right_to_left_out);
}
@Override
protected void onResume() {
super.onResume();
System.out.println("Inside the FolioLevelActivity onResume() method!!!!!!!!!");
}
@Override
protected void onPause() {
super.onPause();
System.out.println("Inside the FolioLevelActivity onPause() method!!!!!!!!!");
}
/** Activity Override - End */
}
//片段1:
public class FirstFragment extends Fragment implements OnClickListener,OnItemClickListener{
private ListView mListView;
/** Override method for drawing the fragment user interface. */
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_folio_level,container, false);
initViews(rootView);
getActivity().getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.right_to_left_in,R.anim.right_to_left_out,R.anim.left_to_right_in,R.anim.left_to_right_out)
.replace(R.id.fragment_container, new SecondFragment().addToBackStack(null).commit();
return rootView;
}
/** This function for handling all widget click events*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.back_button:
getActivity().onBackPressed();
break;
default:
break;
}
}
/** Override - start */
@Override
public void onResume() {
super.onResume();
mListView.setOnItemClickListener(this);
}
@Override
public void onPause() {
super.onPause();
mListView.setOnItemClickListener(null);
}
}
//片段2:
public class SecondFragment extends Fragment implements OnClickListener {
/** This override method for drawing the user interface. */
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_scheme_level,container,false );
return rootView;
}
@Override
public void onClick(View v) {
if(v.getId()== R.id.back_button){
getActivity().getSupportFragmentManager().popBackStack();
}
}
}