活动的后退按钮正在删除片段

时间:2017-11-07 09:33:37

标签: android android-fragments fragment-backstack

我试图保留Fragment的状态。我有MotherActivity其中FragmentOneFragmentTwoFragmentThreeFragmentTwo内部我正在调用另一个活动ChildActivity。问题是当我在ChildActivity中按下后退按钮时,它会刷新MotherActivity而不保持FragmentTwo的状态,而是显示我第一次来的FragmentOne

我只希望当我从BackButtonChildActivity时,容器FragmentTwo应该在那里:

MainAcitivity:

public class MotherActivity extends FragmentActivity {

    AHBottomNavigation bottomNavigation;
    Fragment selectedFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomNavigation = (AHBottomNavigation) findViewById(R.id.navigation);
        bottomNavigation.addItem(item1);
        bottomNavigation.addItem(item2);
        bottomNavigation.addItem(item3);

        bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
            @Override
            public boolean onTabSelected(int position, boolean wasSelected) {

                if (position == 0) {
                    selectedFragment = FragmentOne.newInstance(bottomNavigation);
                } else if (position == 1) {
                    selectedFragment = FragmentTwo.newInstance(bottomNavigation);
                } else if (position == 2) {
                    selectedFragment = FragmentThree.newInstance(bottomNavigation);
                } 

                android.app.FragmentManager fragmentManager = getFragmentManager();
                android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.frame_layout,selectedFragment);
                fragmentTransaction.commit();


                return true;

            }

        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        android.app.FragmentManager fragmentManager = getFragmentManager();
        android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frame_layout, FragmentOne.newInstance(bottomNavigation));
        fragmentTransaction.commit();

    }

    public void setNotification(){
        bottomNavigation.setNotification("1", 1);
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //No call for super().
    }
    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        setResult(113, intent);
        finish();
        return;

    }
}

FragmentTwo我来自childActivity

public class FragmentTwo extends Fragment {
    public static AHBottomNavigation bottomNavigation1;
    Card_detail_Adapter_Footer searchabledapter;
    public static boolean showAddress = false;
    Fragment selectedFragment;

    public static FragmentTwo newInstance(AHBottomNavigation bottomNavigation) {
        FragmentTwo fragment = new FragmentTwo();
        bottomNavigation1 = bottomNavigation;
        showNotificationCounter(3);

        return fragment;
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view   = inflater.inflate(R.layout.fragment_cart, container, false);
        if(showAddress == true){
            showAddress = false;

      // From here I am calling ChildActivity

            Intent intent = new Intent(getActivity(), ChildActivity.class);
            startActivity(intent);

        }
        return view;
    }

}

ChildActivity中简单地调用onBackPressed()

2 个答案:

答案 0 :(得分:1)

问题在于onStart()的{​​{1}}方法,因为当您从MotherActivity.java打开另一项活动时,您的MotherActivity FragmentTwo会被调用。当您通过提交交易时,从onStop()调用ChildActivity的{​​{1}}后退按钮,该按钮正在从onStart()替换MotherActivity。 请查看https://developer.android.com/guide/components/activities/activity-lifecycle.html

答案 1 :(得分:0)

onStart()方法的代码放入onCreate()方法并检查配置是否已更改:

if (savedInstanceState == null) {
    android.app.FragmentManager fragmentManager = getFragmentManager();
    android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.frame_layout, FragmentOne.newInstance(bottomNavigation));
    fragmentTransaction.commit();
}