片段:按下返回按钮时调用哪个回调 &定制它 [英] Fragment: which callback invoked when press back button & customize it

查看:18
本文介绍了片段:按下返回按钮时调用哪个回调 &定制它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段:

公共类 MyFragment 扩展 Fragment{...@覆盖公共视图 onCreateView(...){...}...}

我实例化它:

MyFragment myFragment = new MyFragment();

我用上面的片段来替换当前的片段:

FragmentManager fragmentManager = activity.getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//替换片段fragmentTransaction.replace(R.id.fragment_placeholder, myFragment, "myTag");//注意:我没有添加到后台堆栈

现在,myFragment 显示在屏幕上.注意:我没有将 myFragment 添加到 back stack.

我的两个问题:

1.如果现在我按下手机返回按钮,会调用哪个fragment的生命周期回调??

2.如何自定义MyFragment类中的后退按钮点击监听?(请不要建议我做 myFragment.getView().setOnclickListener,而是在 MyFragment 类中做)

解决方案

问题一:见http://developer.android.com/reference/android/app/Fragment.html#Lifecycle:

<块引用><块引用>

当一个片段不再被使用时,它会经历一系列反向的回调:

onPause() - 片段不再与用户交互,因为它的活动被暂停或片段操作被暂停在活动中修改它.

onStop() - 片段不再对用户可见,因为它的活动正在停止或片段操作正在修改它在活动中.

onDestroyView() - 允许片段清理与其视图关联的资源.

onDestroy() - 调用以对片段的状态进行最终清理.

onDetach() - 在片段不再与其活动关联之前立即调用."

问题 2:如果您必须知道触发回调的是后退按钮具体,您可以在 Fragment 的 Activity 中捕获后退按钮按下并使用你自己的处理方法:

公共类 MyActivity 扩展 Activity{//...//定义在Activity类中,所以重写@覆盖公共无效 onBackPressed(){super.onBackPressed();myFragment.onBackPressed();}}公共类 MyFragment 扩展了 Fragment{//你创建的方法公共无效 onBackPressed(){//处理您在正常生命周期中并不总是希望完成的任何清理工作}}

I have a fragment:

public class MyFragment extends Fragment{
     ...
     @Override
     public View onCreateView(...){...}    
     ...
}

I instantiate it:

MyFragment myFragment = new MyFragment();

I use the above fragment to replace the current fragment:

FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

// replace fragment
fragmentTransaction.replace(R.id.fragment_placeholder, myFragment, "myTag");

// NOTE: I did not add to back stack

Now, myFragment is showing on the screen. NOTE: I did not add myFragment to back stack.

My two questions:

1. If now, I press mobile phone back button, which fragment's life cycle callback will be invoked??

2. How can I customize the back button click listener in MyFragment class? (please do not suggest me to do myFragment.getView().setOnclickListener, but do it in MyFragment class)

解决方案

Question 1: See http://developer.android.com/reference/android/app/Fragment.html#Lifecycle:

"As a fragment is no longer being used, it goes through a reverse series of callbacks:

onPause() - fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.

onStop() - fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.

onDestroyView() - allows the fragment to clean up resources associated with its View.

onDestroy() - called to do final cleanup of the fragment's state.

onDetach() - called immediately prior to the fragment no longer being associated with its activity."

Question 2: If you must know that it was the back button specifically that is triggering the callbacks, You can capture the back button press in your Fragment's Activity and use your own method to handle it:

public class MyActivity extends Activity
{
    //...
    //Defined in Activity class, so override
    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
        myFragment.onBackPressed();
    }
}

public class MyFragment extends Fragment
{
    //Your created method
    public void onBackPressed()
    {
        //Handle any cleanup you don't always want done in the normal lifecycle
    }
}

这篇关于片段:按下返回按钮时调用哪个回调 &amp;定制它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆