N
N
newdancer2016-12-23 14:35:56
Android
newdancer, 2016-12-23 14:35:56

Is there a problem when working with fragments dynamically?

Is there a problem when working with fragments dynamically?
main class:

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();

        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(false);

        // вызываем первый фрагмент regist
        fragment = RegistFragment.newInstance();
        actionBar.setTitle(getResources().getString(R.string.regist_info));
        selectItem(fragment);

        getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            public void onBackStackChanged() {
                int backCount = getSupportFragmentManager().getBackStackEntryCount();
                if (backCount > 1){
                    actionBar.setDisplayHomeAsUpEnabled(true);
                } else {
                    actionBar.setDisplayHomeAsUpEnabled(false);
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 0) {
            getFragmentManager().popBackStack();
        } else {
            finish();
        }
    }

    public void changeFrag(Fragment fragment, String titleFrag) {
        actionBar.setTitle(titleFrag);
        selectItem(fragment);
    }

    private void selectItem(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter, R.anim.fragment_slide_left_exit,
                R.anim.fragment_slide_right_enter, R.anim.fragment_slide_right_exit);
        fragmentTransaction.addToBackStack(fragment.getClass().getName());
        fragmentTransaction.replace(R.id.content_frame, fragment).commit();
    }

When you enter the second fragment, when you press back, the application closes completely, although it should go to the first fragment.
And the second question is how to dynamically change actionBar.setTitle? The question is relevant when we press the back button!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
davidnum95, 2016-12-23
@davidnum95

Try add instead of replace:

fragmentTransaction.add(R.id.content_frame, fragment).commit();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question