从Activity返回后重新创建片段

时间:2014-08-19 10:22:05

标签: android android-fragments fragment

我遇到了与片段生命周期有关的问题。

在此之前,我将设置DONT KEEP ACTIVITY模式(设置 - >开发人员选项)。

在我的项目中有2项活动:

  • 活动1:保留并控制片段A和片段B

  • 活动2:没有片段。

    1. 首先调用活动1,然后将片段A和片段B调用为可见。

    2. 从活动1开始活动2,这导致活动1将被销毁,片段A&片段B也将被销毁(因为不保持活动模式)。

    3. 按活动2中的返回键返回活动1

此处出现问题:从活动2返回后,片段A和片段B将自动调用onCreateView() - >我想避免这种情况。你能给我一些解决方法吗?

更新代码

  • 活动1

     public class MainActivity extends Activity implements OnClickListener {
    
        private Button mBtnShowFragmentA;
    
        private Button mBtnShowFragmentB;
    
        private Button mBtnGoAcitivity2;
    
        protected String mCurrentFragmentTag;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_activity_1);
            mBtnShowFragmentA = (Button) findViewById(R.id.btn_show_fragmentA);
            mBtnShowFragmentB = (Button) findViewById(R.id.btn_show_fragmentB);
            mBtnGoAcitivity2 = (Button) findViewById(R.id.btn_go_activity2);
            mBtnShowFragmentA.setOnClickListener(this);
            mBtnShowFragmentB.setOnClickListener(this);
            mBtnGoAcitivity2.setOnClickListener(this);
    
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
        }
    
        protected void addFragment(int contentId, Fragment fragment, boolean isAddStack,
                String stackName) {
            FragmentManager fm = getFragmentManager();
            String newFragment = fragment.getClass().getName();
    
            FragmentTransaction ft = fm.beginTransaction();
    
            Fragment currentFragment = fm.findFragmentByTag(mCurrentFragmentTag);
    
            if (currentFragment != null && !TextUtils.equals(currentFragment.getTag(), newFragment)) {
                ft.hide(currentFragment);
            }
    
            if (fm.findFragmentByTag(newFragment) != null) {
                fragment = (Fragment) fm.findFragmentByTag(newFragment);
            }
    
            if (!fragment.isAdded()) {
                ft.add(contentId, fragment, newFragment);
            } else {          
                ft.show(fragment);
            }
    
            if (isAddStack) {
                ft.addToBackStack(stackName);
            }
            try {
                ft.commitAllowingStateLoss();
    
            } catch (Exception e) {
    
            }
            mCurrentFragmentTag = newFragment;
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_show_fragmentA:
                    FragmentA fragmentA = new FragmentA();
                    addFragment(R.id.activity_main_content, fragmentA, false, null);
                    break;
                case R.id.btn_show_fragmentB:
                    FragmentB fragmentB = new FragmentB();
                    addFragment(R.id.activity_main_content, fragmentB, false, null);
                    break;
                case R.id.btn_go_activity2:
                    Intent intent = new Intent(getApplicationContext(), SettingActivity.class);
                    startActivity(intent);
    
                    break;
                default:
                    break;
            }
        }
    }
    
  • 片段A

     public class FragmentA extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View view = inflater.inflate(R.layout.layout_fragment_a, container, false);
    
            return view;
        }   
    }
    
  • 片段B

      public class FragmentB extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View view = inflater.inflate(R.layout.layout_fragment_b, container, false);
    
            return view;
        }   
    }
    
  • 活动2

     public class SettingActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_activity_2);
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
        }
    
        @Override
        public void onBackPressed() {
            super.onBackPressed();
        }
    }
    

...................

1 个答案:

答案 0 :(得分:0)

使用以下代码从堆栈中删除活动。

FragmentManager fragmentManager = getSupportFragmentManager();

                if (fragmentManager .getBackStackEntryCount() > 0) {
                    fragmentManager .popBackStack();
                }