片段Android中的Backstack

时间:2017-11-02 09:39:16

标签: android android-fragments fragment-backstack

我正在一个应用程序中工作,我从Fragment1调用Fragment2,当我按下Fragment2中的默认后退按钮时,它将我带到Fragment1但是当我按下任何其他按钮并调用getActivity.finish()时它正在关闭我的活动。

我添加了以下代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    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);

        AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.home,     R.drawable.home, R.color.colorAccent);
        AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.menu,     R.drawable.menu, R.color.colorAccent);
        AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.cart,     R.drawable.cart, R.color.colorAccent);
        AHBottomNavigationItem item4 = new AHBottomNavigationItem(R.string.orders,   R.drawable.orders, R.color.colorAccent);
        AHBottomNavigationItem item5 = new AHBottomNavigationItem(R.string.settings, R.drawable.setting, R.color.colorAccent);

        bottomNavigation.addItem(item1);
        bottomNavigation.addItem(item2);
        bottomNavigation.addItem(item3);
        bottomNavigation.addItem(item4);
        bottomNavigation.addItem(item5);
        bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
        bottomNavigation.setAccentColor(Color.parseColor("#571e19"));

        selectedFragment = ItemHomeFragment.newInstance(bottomNavigation);

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

                if (position == 0) {
                    selectedFragment = ItemHomeFragment.newInstance(bottomNavigation);
                } else if (position == 4) {
                    selectedFragment = Fragment1 (bottomNavigation);
                }

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


                return true;

            }

        });

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



    }

    public void setNotification(){
        bottomNavigation.setNotification("1", 1);
    }

}

Fragment1.java

public class Fragment1 extends Fragment {

    public static AHBottomNavigation bottomNavigation1;
    TextView txtLogout,txtMyProfile,txtTermsCondition,txtRate;
    public int LANGUAGE_REQUEST_CODE = 113;
    private SharedPreferences prefs ;
    Boolean languageBoolean = true;
    MyApplication myApplication;
    private UIView uiView = UIView.getInstance();
    ProgressDialog pDialog;
    static int index;

    public static ItemSettingsFragment newInstance(AHBottomNavigation bottomNavigation) {
        ItemSettingsFragment fragment = new ItemSettingsFragment();
        bottomNavigation1 = bottomNavigation;
        return fragment;
    }

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

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

        View view = inflater.inflate(R.layout.fragment_settings, container, false);
        TextView txtLanguage = (TextView) view.findViewById(R.id.txtLanguage);
        txtLanguage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), LanguageActivity.class);
                startActivity(intent);
            }
        });
                txtMyProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ProfileFragment fragment2 = new ProfileFragment();
                android.app.FragmentManager fragmentManager = getFragmentManager();
                android.app.FragmentTransaction fragmentTransaction =        fragmentManager.beginTransaction();
                fragmentTransaction.replace(android.R.id.content, fragment2);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();


            }
        });

return view;
}

Fragment2.java

public class Fragment2 extends Fragment{

    public Fragment2() { }

    public static Fragment newInstance() {

        Fragment2 profileFragment = new Fragment2();
        return profileFragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.language, container, false);
        TextView fragment2   = (TextView) view.findViewById(R.id.txtLanguage);
        fragment2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();


            }
        });

        return view;
    }


}

1 个答案:

答案 0 :(得分:2)

<强>不

getActivity.finish()

finish()清除活动。

<强>不要

 popBackStack();

示例

FragmentManager fmOBJ= getFragmentManager();
fmOBJ.popBackStack();