3

你能帮我解决这个问题吗?我有带标签的应用程序。每个选项卡都包含自己的片段返回堆栈。当我在用户按下任何选项卡时尝试清除后台堆栈时会出现问题。在调用堆栈活动时,我正在尝试清除堆栈onResume()。但是此解决方案会导致以下错误:

12-18 12:41:41.700: E/AndroidRuntime(4136): FATAL EXCEPTION: main
12-18 12:41:41.700: E/AndroidRuntime(4136): java.lang.RuntimeException: Unable to resume activity {cz.anywhere.fio_t/cz.anywhere.fio_t.fragments.stack.MarketStack}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

这是用于回栈的类。

public class MarketStack extends TabFragmentActivity{

    final public static String MARKET_MAIN_FRAGMENT_TAG = "main_market_fragment";
    final public static String MARKET_SECURITIES_FRAGMENT_TAG = "market_securities_fragment";
    final public static String MARKET_DETAIL_FRAGMENT_TAG = "market_detail_fragment";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i("MarketStack", "onCreate");

        if(savedInstanceState == null){
            addFragmentToStack(new MarketFragment(), MARKET_MAIN_FRAGMENT_TAG);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();

        Log.i("MarketStack", "onResume");

    }

}

和父类。在onResume()方法中发生了错误。

public class TabFragmentActivity extends SherlockFragmentActivity { 

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

            Log.i("TabFragmentActivity", "onCreate");
        }

        public void addFragmentToStack(Fragment newFragment, String fragmentTag) {
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction ft = manager.beginTransaction();
            ft.replace(R.id.content, newFragment, fragmentTag);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.addToBackStack(null);

            ft.commit();
        }

        @Override
        public void onBackPressed() {

            Log.i("TabFragmentActivity", "onBackPressed");

            FragmentManager manager = getSupportFragmentManager();
            if (manager.getBackStackEntryCount() > 1) {
                super.onBackPressed();
            } else {            
                // Otherwise, ask user if he wants to leave :)
                //showExitDialog();
                super.onBackPressed();
            }
        }

        @Override
        protected void onResume() {
            super.onResume();

            FragmentManager fm = getSupportFragmentManager();

            for(int i = 0; i<fm.getBackStackEntryCount()-1; i++){
                fm.popBackStack();
            }
        }

谢谢你的建议。

4

0 回答 0