How to implement onBackPressed() in Fragments?

2018-12-31 03:15发布

Is there a way in which we can implement onBackPressed() in Android Fragment similar to the way in which we implement in Android Activity?

As the Fragment lifecycle do not have onBackPressed(). Is there any other alternative method to over ride onBackPressed() in Android 3.0 fragments?

30条回答
像晚风撩人
2楼-- · 2018-12-31 04:06

None of that is easy to implement nor will it function in an optimal way.

Fragments have a method call onDetach that will do the job.

@Override
    public void onDetach() {
        super.onDetach();
        PUT YOUR CODE HERE
    }

THIS WILL DO THE JOB.

查看更多
心情的温度
3楼-- · 2018-12-31 04:06

Just follow these steps:

Always while adding a fragment,

fragmentTransaction.add(R.id.fragment_container, detail_fragment, "Fragment_tag").addToBackStack(null).commit();

Then in the main activity, override onBackPressed()

if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
    getSupportFragmentManager().popBackStack();
} else {
    finish();
}

To handle the back button in your app,

Fragment f = getActivity().getSupportFragmentManager().findFragmentByTag("Fragment_tag");
if (f instanceof FragmentName) {
    if (f != null) 
        getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit()               
}

That's it!

查看更多
爱死公子算了
4楼-- · 2018-12-31 04:07

This is just a small code that will do the trick:

 getActivity().onBackPressed();

Hope it helps someone :)

查看更多
墨雨无痕
5楼-- · 2018-12-31 04:07

I have used another approach as follows:

  • An Otto event bus to communicate between the Activity and its Fragments
  • A Stack in the Activity containing custom back actions wrapped in a Runnable that the calling fragment defines
  • When onBackPressed is called in the controlling Activity, it pops the most recent custom back action and executes its Runnable. If there's nothing on the Stack, the default super.onBackPressed() is called

The full approach with sample code is included here as an answer to this other SO question.

查看更多
牵手、夕阳
6楼-- · 2018-12-31 04:07

public class MyActivity extends Activity {

protected OnBackPressedListener onBackPressedListener;

public interface OnBackPressedListener {
    void doBack();
}

public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
    this.onBackPressedListener = onBackPressedListener;
}

@Override
public void onBackPressed() {
    if (onBackPressedListener != null)
        onBackPressedListener.doBack();
    else
        super.onBackPressed();
} 

@Override
protected void onDestroy() {
    onBackPressedListener = null;
    super.onDestroy();
}

}

in your fragment add the following, dont forget to implement mainactivity's interface.

public class MyFragment extends Framgent implements MyActivity.OnBackPressedListener {
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
         ((MyActivity) getActivity()).setOnBackPressedListener(this);
    }

@Override
public void doBack() {
    //BackPressed in activity will call this;
}

}
查看更多
姐姐魅力值爆表
7楼-- · 2018-12-31 04:08

How about using onDestroyView()?

@Override
public void onDestroyView() {
    super.onDestroyView();
}
查看更多
登录 后发表回答