使用onBackPressed功能实现嵌套片段

时间:2019-01-18 17:30:06

标签: android android-fragments

我想在我的项目中实现此源代码而不使用ViewPager。实现onBackPressedMainActivity上不起作用之后,当我提交其他Fragment时,但是按下后退按钮导致我的应用程序退出并且onBackPressed无法正常工作< / p>

MainActivity onBackPressed()

@Override
public void onBackPressed() {
    if (!homeFragment.onBackPressed()) {
        // container Fragment or its associates couldn't handle the back pressed task
        // delegating the task to super class
        super.onBackPressed();

    } else {
        // carousel handled the back pressed task
        // do not call super
    }
}

MainActivity类:

public class MainActivity extends AppCompatActivity{

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

        ...
        if (savedInstanceState == null) {
            initScreen();
        } else {
            homeFragment = (HomeFragment) getSupportFragmentManager().getFragments().get(0);
        }
        ...

    }

    private void initScreen() {
        // Creating the ViewPager container fragment once
        homeFragment = new HomeFragment();

        final FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.fragments_container, homeFragment)
                .commit();
    }

    @Override
    public void onBackPressed() {
        if (!homeFragment.onBackPressed()) {
            // container Fragment or its associates couldn't handle the back pressed task
            // delegating the task to super class
            super.onBackPressed();

        } else {
            // carousel handled the back pressed task
            // do not call super
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
}

HomeFragment类:

public class HomeFragment extends Fragment implements View.OnClickListener{

    ...
    private Fragment currentFragment;
    SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.instagram_post_container, container, false);

        //FORCE ATTACH OTHER FRAGMENT
        currentFragment = new SampleFragment();
        registeredFragments.put(0, currentFragment);
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.addToBackStack(null);
        transaction.replace(R.id.root_container, currentFragment).commit();

        return mView;
    }


    public boolean onBackPressed() {
        OnBackPressListener thisFragment = (OnBackPressListener) registeredFragments.get(0);
        if (currentFragment != null) {
            // lets see if the currentFragment or any of its childFragment can handle onBackPressed
            return thisFragment.onBackPressed();
        }

        // this Fragment couldn't handle the onBackPressed call
        return false;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //Bundle arguments = getArguments();
    }
}

SampleFragment类:

public class SampleFragment extends RootFragment {
    ...

    public SampleFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_instagram_profile, container, false);

        return mView;
    }
}

RootFragment类:

public class RootFragment extends Fragment implements OnBackPressListener {
    @Override
    public boolean onBackPressed() {
        return new BackPressImpl(this).onBackPressed();
    }
}

然后是OnBackPressListener界面

public interface OnBackPressListener {
    boolean onBackPressed();
}

问题

HomeFragment

thisFragment.onBackPressed()

始终在部分代码中返回false:

    OnBackPressListener thisFragment = (OnBackPressListener) registeredFragments.get(0);
    if (currentFragment != null) {
        // lets see if the currentFragment or any of its childFragment can handle onBackPressed
        return thisFragment.onBackPressed();
    }

0 个答案:

没有答案