java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

转载自:https://blog.csdn.net/dsc114/article/details/44857685

首先描述下所要实现的功能点:

MainActivity使用Fragment实现底部菜单,底部共有四个菜单按钮,分别对应:AFragment,BFragment,CFragment,DFragment。其中AFragment是默认显示。

点击CFragment中的一个button后跳转到第二个Activity界面:SecondActivity。

SecondActivity返回键有两个:button01、button02.其中button01返回的是CFragment;button02返回的是AFragment。

问题出现了:

button01可以返回到MainActivity中的CFragment,但是button02却没实现返回到MainActivity中的AFragment,无论是在SecondActivity中使用finish(),还是使用Intent跳转,都不能实现返回到MainActivity中的AFragment。

最后想到了广播机制来实现(我总感觉应该有更简单更好的方法,却始终没找到,迫不得已使用广播机制)。

去出现了另外一个问题:

Fragment java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState。

最后fragmentTransaction.commitAllowingStateLoss()替换掉fragmentTransaction.commit()解决问题。



最后再浏览下代码:

[java]  view plain  copy
  1. import java.util.ArrayList;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.app.Activity;  
  5. import android.app.AlertDialog;  
  6. import android.app.Dialog;  
  7. import android.content.BroadcastReceiver;  
  8. import android.content.Context;  
  9. import android.content.DialogInterface;  
  10. import android.content.Intent;  
  11. import android.content.IntentFilter;  
  12. import android.graphics.Color;  
  13. import android.graphics.drawable.Drawable;  
  14. import android.os.Bundle;  
  15. import android.support.v4.app.FragmentManager;  
  16. import android.support.v4.app.FragmentTransaction;  
  17. import android.view.MotionEvent;  
  18. import android.view.View;  
  19. import android.view.View.OnClickListener;  
  20. import android.widget.ImageButton;  
  21. import android.widget.TextView;  
  22.   
  23. import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;  
  24. import com.nostra13.universalimageloader.core.ImageLoader;  
  25. import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;  
  26. import com.nostra13.universalimageloader.core.assist.QueueProcessingType;  
  27.   
  28. public class MainActivity extends BaseActivity {  
  29.     private static final String LOG_TAG = MainActivity.class.getSimpleName();  
  30.       
  31.     private FragmentManager fragmentManager;  
  32.     private TextView txt_home;  
  33.     private TextView txt_find;  
  34.     private TextView txt_collection;  
  35.     private TextView txt_setting;  
  36.       
  37.     private FragmentTransaction transaction;  
  38.     private HomeFragment homeFragment;  
  39.     private FindFragment findFragment;  
  40.     private CollectionFragment collectionFragment;  
  41.     private SettingFragment settingFragment;  
  42.     private ImageButton imgbtn_voice;  
  43.     private final static int SCANNIN_GREQUEST_CODE = 65537;  
  44.   
  45.     private BackBroadcast backBroadcast;  
  46.       
  47.     @Override  
  48.     protected void onCreate(Bundle savedInstanceState) {  
  49.         super.onCreate(savedInstanceState);  
  50.         ResourceHelper.activityContext = this;  
  51.   
  52.         setContentView(R.layout.activity_main);  
  53.   
  54.         initImageLoaderConfi();  
  55.   
  56.         initView();  
  57.           
  58.               
  59.     }  
  60.   
  61.     /** 
  62.      * 底部菜单切换 
  63.      */  
  64.     private void initView() {  
  65.         fragmentManager = getSupportFragmentManager();  
  66.           
  67.         txt_home       = (TextView) findViewById(R.id.home_text);  
  68.         txt_find       = (TextView) findViewById(R.id.find_text);  
  69.         txt_collection = (TextView) findViewById(R.id.collection_text);  
  70.         txt_setting    = (TextView) findViewById(R.id.setting_text);  
  71.           
  72.         txt_home.setOnClickListener(new OnClickListener() {  
  73.   
  74.             @Override  
  75.             public void onClick(View v) {  
  76.                 setTabSelection(0);  
  77.             }  
  78.         });  
  79.         txt_find.setOnClickListener(new OnClickListener() {  
  80.   
  81.             @Override  
  82.             public void onClick(View v) {  
  83.                 setTabSelection(1);  
  84.             }  
  85.         });  
  86.         txt_collection.setOnClickListener(new OnClickListener() {  
  87.   
  88.             @Override  
  89.             public void onClick(View v) {  
  90.                 setTabSelection(2);  
  91.             }  
  92.         });  
  93.         txt_setting.setOnClickListener(new OnClickListener() {  
  94.   
  95.             @Override  
  96.             public void onClick(View v) {  
  97.                 setTabSelection(3);  
  98.             }  
  99.         });  
  100.         setTabSelection(0);  
  101.     }  
  102.   
  103.     @SuppressLint("NewApi")  
  104.     public void setTabSelection(int index) {  
  105.         clearSelection();  
  106.         transaction = fragmentManager.beginTransaction();  
  107.         hideFragments(transaction);  
  108.         switch (index) {  
  109.         case 0:  
  110.             // 当点击了tab时,改变控件的图片和文字颜色  
  111.             Drawable drawable01 = this.getResources().getDrawable(R.drawable.home_home_pressed);  
  112.             txt_home.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawable01, nullnull);  
  113.             txt_home.setTextColor(Color.parseColor("#2786c8"));  
  114.             if (homeFragment == null) {  
  115.                 homeFragment = new HomeFragment();  
  116.                 transaction.add(R.id.content, homeFragment);  
  117.             } else {  
  118.                 transaction.show(homeFragment);  
  119.             }  
  120.             break;  
  121.         case 1:  
  122.             Drawable drawable02 = this.getResources().getDrawable(R.drawable.home_find_pressed);  
  123.             txt_find.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawable02, nullnull);  
  124.             txt_find.setTextColor(Color.parseColor("#2786c8"));  
  125.             if (findFragment == null) {  
  126.                 findFragment = new FindFragment();  
  127.                 transaction.add(R.id.content, findFragment);  
  128.             } else {  
  129.                 transaction.show(findFragment);  
  130.             }  
  131.             break;  
  132.         case 2:  
  133.             Drawable drawable03 = this.getResources().getDrawable(R.drawable.home_collection_pressed);  
  134.             txt_collection.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawable03, nullnull);  
  135.             txt_collection.setTextColor(Color.parseColor("#2786c8"));  
  136.             if (collectionFragment == null) {  
  137.                 collectionFragment = new CollectionFragment();  
  138.                 transaction.add(R.id.content, collectionFragment);  
  139.             } else {  
  140.                 transaction.show(collectionFragment);  
  141.             }  
  142.             break;  
  143.         case 3:  
  144.             Drawable drawable04 = this.getResources().getDrawable(R.drawable.home_setting_pressed);  
  145.             txt_setting.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawable04, nullnull);  
  146.             txt_setting.setTextColor(Color.parseColor("#2786c8"));  
  147.             if (settingFragment == null) {  
  148.                 settingFragment = new SettingFragment();  
  149.                 transaction.add(R.id.content, settingFragment);  
  150.             } else {  
  151.                 transaction.show(settingFragment);  
  152.             }  
  153.             break;  
  154.         }  
  155.         transaction.commitAllowingStateLoss();   //该处是重点,如果使用transaction.commit()会一直崩溃报错。  
  156.     }  
  157.   
  158.     // 每次选中之前先清楚掉上次的选中状态  
  159.     @SuppressLint("NewApi")  
  160.     private void clearSelection() {  
  161.         txt_home.setTextColor(Color.parseColor("#82858b"));  
  162.         Drawable drawableHome = this.getResources().getDrawable(R.drawable.home_home_normal);  
  163.         txt_home.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawableHome, nullnull);  
  164.           
  165.         txt_find.setTextColor(Color.parseColor("#82858b"));  
  166.         Drawable drawableFind = this.getResources().getDrawable(R.drawable.home_find_normal);  
  167.         txt_find.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawableFind, nullnull);  
  168.           
  169.         txt_collection.setTextColor(Color.parseColor("#82858b"));  
  170.         Drawable drawableMy = this.getResources().getDrawable(R.drawable.home_collection_normal);  
  171.         txt_collection.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawableMy, nullnull);  
  172.           
  173.         txt_setting.setTextColor(Color.parseColor("#82858b"));  
  174.         Drawable drawableMore = this.getResources().getDrawable(R.drawable.home_setting_normal);  
  175.         txt_setting.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawableMore, nullnull);  
  176.     }  
  177.     // 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况  
  178.     private void hideFragments(FragmentTransaction transaction) {  
  179.         if (homeFragment != null) {  
  180.             transaction.hide(homeFragment);  
  181.         }  
  182.         if (findFragment != null) {  
  183.             transaction.hide(findFragment);  
  184.         }  
  185.         if (collectionFragment != null) {  
  186.             transaction.hide(collectionFragment);  
  187.         }  
  188.         if (settingFragment != null) {  
  189.             transaction.hide(settingFragment);  
  190.         }  
  191.     }  
  192.       
  193.     @Override  
  194.     protected void onResume() {  
  195.         super.onResume();  
  196. //      setTabSelection(0);  
  197.           
  198.         backBroadcast = new BackBroadcast();  
  199.         registerReceiver(backBroadcast, new IntentFilter(Constant.HOME_BACK_ACTION));  
  200.     }  
  201.       
  202.     @Override  
  203.     protected void onDestroy() {  
  204.         super.onDestroy();  
  205.         unregisterReceiver(backBroadcast);  
  206.     }  
  207.     private class BackBroadcast extends BroadcastReceiver{  
  208.   
  209.         @Override  
  210.         public void onReceive(Context context, Intent intent) {  
  211.             String temp = intent.getAction();  
  212.             if (temp.equals(Constant.HOME_BACK_ACTION)) {  
  213.                 setTabSelection(0);  
  214.             }  
  215.         }  
  216.     }  
  217.       
  218.     @Override  
  219.     protected void onSaveInstanceState(Bundle outState) {  
  220.         super.onSaveInstanceState(outState);  
  221.     }  
  222. }  

顺便把布局也贴出来吧:

[html]  view plain  copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <FrameLayout  
  9.         android:id="@+id/content"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="0dp"  
  12.         android:layout_weight="1" >  
  13.     </FrameLayout>  
  14.   
  15.     <LinearLayout  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:background="@drawable/home_bottom"  
  19.         android:baselineAligned="true" >  
  20.   
  21.         <TextView  
  22.             android:id="@+id/home_text"  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:layout_gravity="center"  
  26.             android:layout_marginTop="10dip"  
  27.             android:layout_weight="1"  
  28.             android:drawableTop="@drawable/home_home"  
  29.             android:gravity="center"  
  30.             android:text="首页"  
  31.             android:textColor="#82858b" />  
  32.   
  33.         <TextView  
  34.             android:id="@+id/find_text"  
  35.             android:layout_width="wrap_content"  
  36.             android:layout_height="wrap_content"  
  37.             android:layout_gravity="center"  
  38.             android:layout_marginTop="10dip"  
  39.             android:layout_weight="1"  
  40.             android:drawableTop="@drawable/home_find"  
  41.             android:gravity="center"  
  42.             android:text="发现"  
  43.             android:textColor="#82858b" />  
  44.   
  45.         <TextView  
  46.             android:id="@+id/collection_text"  
  47.             android:layout_width="wrap_content"  
  48.             android:layout_height="wrap_content"  
  49.             android:layout_gravity="center"  
  50.             android:layout_marginTop="10dip"  
  51.             android:layout_weight="1"  
  52.             android:drawableTop="@drawable/home_collection"  
  53.             android:gravity="center"  
  54.             android:text="收藏"  
  55.             android:textColor="#82858b" />  
  56.   
  57.         <TextView  
  58.             android:id="@+id/setting_text"  
  59.             android:layout_width="wrap_content"  
  60.             android:layout_height="wrap_content"  
  61.             android:layout_gravity="center"  
  62.             android:layout_marginTop="10dip"  
  63.             android:layout_weight="1"  
  64.             android:drawableTop="@drawable/home_setting"  
  65.             android:gravity="center"  
  66.             android:text="设置"  
  67.             android:textColor="#82858b" />  
  68.     </LinearLayout>  
  69.   
  70. </LinearLayout>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值