我被這個問題困住了。我用刷卡切換片段。onSaveInstanceState在屏幕方向更改後無法執行此操作
上向左滑動:
public void nextFragment() {
if(collection.iterator(). hasNext()){
String fragmentNameToHide = collection.iterator().current();
String fragmentNameToShow = collection.iterator().next();
Fragment fragmentToShow = null;
if((fragmentToShow=fragmentManager.findFragmentByTag(fragmentNameToShow))==null){
Class<?> className=null;
try {
className = Class.forName(fragmentNameToShow);
Constructor<?> constructor = className.getConstructor();
Object object = constructor.newInstance();
fragmentToShow = (Fragment) object;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.animator.slide_in_right, R.animator.slide_out_left);
Fragment fragmentToHide = fragmentManager.findFragmentByTag(fragmentNameToHide);
ft.add(R.id.fragment_container, fragmentToShow, fragmentNameToShow);
ft.show(fragmentToShow);
ft.hide(fragmentToHide);
ft.commitAllowingStateLoss();
}
}
上向右滑動即可:
public boolean prevFragment() {
if(collection.iterator().isLast()){
Log.i("prv", "son");
String fragmentNameToRemove = collection.iterator().current();
String fragmentNameToShow = collection.getFragmentList().get(0);
Fragment fragmentToShow = fragmentManager.findFragmentByTag(fragmentNameToShow);
Fragment fragmentToRemove2 = fragmentManager.findFragmentByTag(collection.getFragmentList().get(1));
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
Fragment fragmentToRemove = fragmentManager.findFragmentByTag(fragmentNameToRemove);
ft.hide(fragmentToRemove);
ft.hide(fragmentToRemove2);
ft.show(fragmentToShow);
ft.commit();
collection.iterator().setPosition(0);
Delict.getInstance().releaseDelict();
return false;
}else{
Log.i("prv", "son değil");
if (collection.iterator().hasPrev()) {
String fragmentNameToRemove = collection.iterator().current();
String fragmentNameToShow = collection.iterator().prev();
Fragment fragmentToShow = fragmentManager.findFragmentByTag(fragmentNameToShow);
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
Fragment fragmentToRemove = fragmentManager.findFragmentByTag(fragmentNameToRemove);
ft.show(fragmentToShow);
ft.hide(fragmentToRemove);
ft.commit();
return true;
}else{
android.os.Process.killProcess(android.os.Process.myPid());
return false;
}
}
}
這裏是我的代碼: 我的主要活動:
public class DelictActivity extends Activity implements SimpleGestureListener {
private SimpleGestureFilter detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delict);
detector = new SimpleGestureFilter(this,this, getFragmentManager());
if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT){
Fragment fragment = null;
FragmentTransaction ft = getFragmentManager().beginTransaction();
if((fragment=getFragmentManager().findFragmentByTag("com.example.PlateFragment"))==null){
PlateFragment plate = new PlateFragment();
ft.add(R.id.fragment_container, plate, "com.example.PlateFragment");
ft.show(plate);
}else{
ft.show(fragment);
}
ft.commit();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent me){
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
}
我弗里斯特片段:
public class PlateFragment extends Fragment {
public PlateFragment() {
}
Button enterPlate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_plate, null);
enterPlate = (Button) view.findViewById(R.id.openPlateLayer);
enterPlate.setOnClickListener(openPlateLayerListener);
return view;
}
OnClickListener openPlateLayerListener = new OnClickListener() {
@Override
public void onClick(View v) {
PlateDialogFragment newFragment = PlateDialogFragment.newInstance();
newFragment.addListener(dialogFragmentListener);
newFragment.show(getFragmentManager(),"dialog");
}
};
DialogFragmentListener dialogFragmentListener = new DialogFragmentListener() {
@Override
public void onSuccess() {
Delict delict =Delict.getInstance();
enterPlate.setText(delict.getPlate());
}
};
}
我的第二個片段:
public class CategoryFragment extends Fragment {
public CategoryFragment() {
}
TextView text;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_category, null);
Button categoryLayerButton = (Button) view.findViewById(R.id.selectCategory);
categoryLayerButton.setOnClickListener(categoryLayerButtonListener);
text = (TextView) view.findViewById(R.id.text);
return view;
}
OnClickListener categoryLayerButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
CategoryDialogFragment newFragment = CategoryDialogFragment.newInstance();
newFragment.addListener(dialogFragmentListener);
newFragment.show(getFragmentManager(),"dialog");
}
};
DialogFragmentListener dialogFragmentListener = new DialogFragmentListener() {
@Override
public void onSuccess() {
Delict delict = Delict.getInstance();
if(delict.getCategoryList().size()>1){
text.setText("ihlallerini yaptı;");
}else if(delict.getCategoryList().size()==1){
text.setText("ihlalini yaptı;");
}
loadCategoryImages();
}
};
private void loadCategoryImages() {
view.findViewById(R.id.selectCategory).setVisibility(View.GONE);
LinearLayout stack = (LinearLayout) view.findViewById(R.id.categoryLayout);
Delict delict = Delict.getInstance();
for(int i = 0; i<delict.getCategoryList().size(); i++){
ImageView image = new ImageView(getActivity());
image.setBackgroundResource(R.drawable.ic_launcher);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins((-30*i), 0, 0, 0);
image.setLayoutParams(params);
stack.addView(image);
}
}
private static final String FRAGMENT_KEY = "fragmentkey";
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean(FRAGMENT_KEY, true);
super.onSaveInstanceState(outState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
if(savedInstanceState.getBoolean(FRAGMENT_KEY)){
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment frag1 = getFragmentManager().findFragmentByTag("com.example.PlateFragment");
ft.hide(frag1);
ft.commitAllowingStateLoss();
}
}
}
}
logcat的說:
03-13 15:01:43.320: E/AndroidRuntime(8477): FATAL EXCEPTION: main
03-13 15:01:43.320: E/AndroidRuntime(8477): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1265)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1276)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.app.BackStackRecord.commit(BackStackRecord.java:525)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.example.fragmentmanager.FragmentCollection.prevFragment(FragmentCollection.java:77)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.example.SimpleGestureFilter.onFling(SimpleGestureFilter.java:130)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:579)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.example.SimpleGestureFilter.onTouchEvent(SimpleGestureFilter.java:53)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.example.activities.DelictActivity.dispatchTouchEvent(DelictActivity.java:45)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1897)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.view.View.dispatchPointerEvent(View.java:5726)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2922)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2498)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:864)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2507)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.os.Looper.loop(Looper.java:137)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.app.ActivityThread.main(ActivityThread.java:4447)
03-13 15:01:43.320: E/AndroidRuntime(8477): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 15:01:43.320: E/AndroidRuntime(8477): at java.lang.reflect.Method.invoke(Method.java:511)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-13 15:01:43.320: E/AndroidRuntime(8477): at dalvik.system.NativeStart.main(Native Method)
而第三個片段是類似的東西。如您所見,在活動中的onCreate
方法中,首先將片段添加到容器中。我有2 xml的主要活動。第一個是縱向模式,另一個是橫向模式。如果方向是縱向,則屏幕方向會改變,其中一個片段將顯示在屏幕上。如果方向是橫向的,則總是應該有相同的佈局 - 用於橫向模式的佈局。
但在第二次活動屏幕方向更改後滑動時,出現此錯誤。任何人都可以幫助我?
什麼logcat的說 –
@ConstantinCerberus請看到我的編輯。 – MSr