The Android fragment superimposed by another fragment is still isShown, not onPause

In my application, I will catch onBackPressedin MainActivityand send the broadcast to fragments. The receiver in each fragment turns on or off with

@Override
public void onPause() {
    super.onPause();
    getActivity().unregisterReceiver(receiver);

    Bundle args = getArguments();
    int tab = args.getInt("Tab");
    Log.d(log, String.format("Tab1Fragment %d onPause", tab));

}

@Override
public void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter("de.foo.BackPressed");
    receiver = new MyReceiver();
    getContext().registerReceiver(receiver, filter);

    Bundle args = getArguments();
    int tab = args.getInt("Tab");
    Log.d(log, String.format("Tab1Fragment %d onResume", tab));

}

In addition, in the broadcast receiver, I check the visibility of the fragment to make sure that only the visible fragment can handle the event BackPressed:

if (intent.getAction().equals("de.foo.BackPressed")) {
    if (getView() != null && getView().isShown()) {
        ...
    }
}

When I replace an existing and visible fragment with another, onPause()it is not called on the replaced fragment. Its broadcast receiver is still on. Also the receiver in a new fragment. Both fragments give true for getView().isShown(), but this is not true: only the last fragment is visible.

How can I achieve that only the visible fragment reacts?

EDIT: 0 . "Tab1Fragment 0 onResume" . , html. :

            HTML_Fragment newFragment = new HTML_Fragment();
            newFragment.setArguments(extras);
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.content, newFragment);
            ft.addToBackStack(null);
            ft.commit();

HTML_Fragment , "HTMLFragment 0 onResume". HTML_, "Tab1Fragment 0 onPause". . .

+4
3

, , :

  • , , :

     // to show the root fragment
     getSupportFragmentManager().beginTransaction().
                    replace(R.id.container, new FragmentA()).
                    commit();
     ....
     // to show subfragment (i.e. fragment which is replacing the root fragment)
     getSupportFragmentManager().beginTransaction().
                    replace(R.id.container, new FragmentB()).addToBackStack(null).
                    commit();
    

    onPause() FragmentA onResume() FragmentB ( - onPause() FragmentBonResume() FragmentA).

replace() - , remove(Fragment) , , add(int, Fragment, String) . add() - .

add() ( , show()/hide()) - .

, .

0

. onResume onPause in Fragment Activity onResume onPause.

, . , Pause , onResume.

Fragment.isVisible(), , .

isVisible() .

        HTML_Fragment newFragment = new HTML_Fragment();
        newFragment.setArguments(extras);
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        // you can add a tag when add fragment ,and findByTag() to find the fragment.
        ft.remove(mTab1Fragment);

        ft.replace(R.id.content, newFragment);
        //ft.addToBackStack(null);
        ft.commit();
0

The problem is not solved. I ensured with the help of the parameter system that only the current visible fragment is processing the translation.

0
source

Source: https://habr.com/ru/post/1623528/


All Articles