Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

Using onBackPressed() in Android Fragments

I am working on a project and I need to be able to use the back button in each fragment to navigate between previous fragments, I have methods written to do so by using a back arrow in the action bar, however, I want to be able to use the same functionality on the back button pressed. I don't want to use the back stack. Is there a way to do this?

EDIT

Rather than using the back stack I want to be able to call the go back to previous method below when the user clicks the back button. I need to used the gobackpressed method within fragments. Is this possible? I hope this is clear and concise. Apologies for any confusion caused above.

Go Back to Previous

public void gobackToPreviousFragment(String preFragmentTag, Fragment preFragment){

    FragmentManager  fm = getSupportFragmentManager();

    FragmentTransaction ft = fm.beginTransaction();
    ft.setCustomAnimations(R.animator.close_slide_in,R.animator.close_slide_out);

    ft.show(preFragment);

    //**BY REMOVING FRAGMENT, WHEN USER TRIES TO REVISIT, FRAGMENT IS BLACK**

    ft.remove(fm.findFragmentByTag(Misc.currentContentFragmentTag));
    ft.addToBackStack(null);
    ft.commit();

    Misc.currentContentFragmentTag = preFragmentTag;

    createBar(preFragment);
}

Go Forward

public void gotoNextFragment(String nextTag, Fragment nextFragment){

    FragmentManager  fm = getSupportFragmentManager();

    FragmentTransaction ft = fm.beginTransaction();
    ft.setCustomAnimations(R.animator.enter_slide_in, R.animator.enter_slide_out);

    boolean newlyCreated = false;
    if(nextFragment == null){
        nextFragment = Fragment.instantiate(this, nextTag);
        newlyCreated = true;
    }

    //hide current fragment
    ft.hide(fm.findFragmentByTag(Misc.currentContentFragmentTag));

    if(newlyCreated){
        ft.add(R.id.content_frame, nextFragment, nextTag);
    }
    else{
        ft.show(nextFragment);
    }

    ft.addToBackStack(null);
    ft.commit();
    Misc.currentContentFragmentTag = nextTag;

    createBar(nextFragment);
}

These are how I navigate back and forth, and I'd like to be able to implement the go back method on the onBackPressed(). Does this make sense?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I didn't find any good answer about this problem, so this is my solution.

If you want to get backPress in each fragment do the following.

create interface OnBackPressedListener

public interface OnBackPressedListener {
    void onBackPressed();
}

That each fragment that wants to be informed of backPress implements this interface.

In parent activity , you can override onBackPressed()

@Override
public void onBackPressed() {
    List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
    if (fragmentList != null) {
        //TODO: Perform your logic to pass back press here
        for(Fragment fragment : fragmentList){
           if(fragment instanceof OnBackPressedListener){
               ((OnBackPressedListener)fragment).onBackPressed();
           }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...