Fragment中的返回栈(Backstack)

3

我正在一个应用程序中工作,我从Fragment1调用Fragment2,当我在Fragment2中按默认返回按钮时,它会带我回到Fragment1,但是当我按其他按钮并调用getActivity.finish()时,它会关闭我的Activity。

我已经添加了下面的代码:

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
finish() 将销毁该活动。 - Peter Haddad
1个回答

2

不要

getActivity.finish()

finish() clear the Activity.

Do

 popBackStack();

示例

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

2
你好,关于编程方面的问题,我想问一下,从popBackStack返回时,有没有办法将数据从Fragment2发送到Fragment1?还是需要采取其他措施? - Usman Khan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接