Fragment进阶(五)----->监听fragment回退事件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/btn_add_frg3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="add frag3" /> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
fragment3.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0000ff" android:orientation="vertical"> <TextView android:id="@+id/fragment3_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25dp" android:text="这是Fragment 3" /> </LinearLayout>
MainActivity
package com.harvic.fragmentback; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.Button; import android.widget.Scroller; /** * 既然清楚了Fragment仅仅是一个控件,而回退事件也仅仅能在Activity中拦截。那我们就能够想办法了。 * 首先,我们能够在Fragment类中咱们自己写一个onBackPressed()方法来处理回调事件。 * 然后。能够利用回调。将要处理回退事件的fragment实例。传给Activity。 * 最后,在拿到fragment实例以后,就能够在Activity的onBackPress * ()方法中。调用这个fragment实例的onBackPressed()方法了。 这样。我们就在fragment中拦截了回退事件了。* */ public class MainActivity extends FragmentActivity implements View.OnClickListener, Fragment3.BackHandlerInterface { private Fragment3 selectedFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnAddFrag3 = (Button) findViewById(R.id.btn_add_frg3); btnAddFrag3.setOnClickListener(this); } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.btn_add_frg3: { Fragment3 fragment3 = new Fragment3(); addFragment(fragment3, "fragment3"); } break; } } private void addFragment(Fragment fragment, String tag) { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.add(R.id.fragment_container, fragment, tag); transaction.addToBackStack(tag); transaction.commit(); } /** * 在activity中获取fragment页面 */ @Override public void setSelectedFragment(Fragment3 backHandledFragment) { this.selectedFragment = backHandledFragment; } /** * !selectedFragment.onFragmentBackPressed()返回true(non-Javadoc) * selectedFragment.onFragmentBackPressed()返回false; 让activity来处理就可以 */ @Override public void onBackPressed() { if (!selectedFragment.onFragmentBackPressed()) { // 回退 super.onBackPressed(); } } }
Fragment3
package com.harvic.fragmentback; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment3 extends Fragment { private boolean mHandledPress = false; protected BackHandlerInterface backHandlerInterface; public interface BackHandlerInterface { public void setSelectedFragment(Fragment3 backHandledFragment); } TextView tv; /** * 事实上在onAttach()回调时就已经把Fragment与Activity绑定在了一起,所以仅仅要生命流程在onAttach() * 之后的随意一个生命周期 * ,我们都能够通过getActivity来获取Activity的实例,来进行强制转换,所以在这里我们就换个地方,在onCreate()函数中来做: */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!(getActivity() instanceof BackHandlerInterface)) { throw new ClassCastException( "Hosting activity must implement BackHandlerInterface"); } else { // 强转为接口实例 backHandlerInterface = (BackHandlerInterface) getActivity(); } } /** * onCreateView返回一个fragment视图 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment3, container, false); } /** * 有关这个设置Fragment3实例的代码,仅仅要在生命周期中Fragment3实例已经产生了都能够设置,即能够放在生命周期在onCreate() * 后的函数里,即onCreate()、onCreateView()、onActivityCreated()、onStart()。尽管经过我測试。 * 放在这几个函数中的随意一个都是可行的 * ,但onActivityCreated()后才是Activity终于onCreate()运行完。所以放在onActivityCreated * ()或onStart()中是最保险的。所以这里放在了onStart()中来处理 */ @Override public void onStart() { super.onStart(); backHandlerInterface.setSelectedFragment(this); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); tv = (TextView) getView().findViewById(R.id.fragment3_tv); } /** * 其返回一个布尔值;意思是,假设对返回事件进行了处理就返回TRUE,假设不做处理就返回FALSE,让上层进行处理。 * * @return */ public boolean onFragmentBackPressed() { if (!mHandledPress) { tv.setText("Fragment3 \n 捕捉到了回退事件哦!"); mHandledPress = true; return true; } return false; } }