0

FragmentA respond to onConfigurationChanged correctly but if i open FragmentB on top of it and rotate the device it's onConfigurationChanged triggered and also triggered onConfigurationChanged of FragmentA.

I have fragmentA in activity and FragmentA respond to onConfigurationChanged with in it and open another activity on landscape mode and if click on FragmentA list its open detail fragment lets call it fragmentB. fragmentB also have method onConfigurationChanged and open another activity in landscape mode.

FragmentA{


@Override
    public void onClick(View v) {
         //open FragmentB 
    }

public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

       } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
          //landScape of FragmentA 
          //open any Activaty
        }

    }

FragmentB{

public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

       } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
          //landScape of FragmentB
          //open any Activaty
        }

    }

orientation change on fragmentB cause open two activity's .One from onConfigurationChanged of fragmentA and one from onConfigurationChanged of FragmentB.

Wahab Khan Jadon
  • 875
  • 13
  • 21
  • Are both fragment A and B on the same activity? If yes, it's best to handle configuration change from the activity and alter your response to the change based on the active fragment. –  Sep 13 '19 at 08:43
  • Please refer this [solution](https://stackoverflow.com/questions/18375288/fragment-lifecycle-which-method-is-called-upon-show-hide/18375436#18375436) Use can Use setUserVisibleHint. – Govind Sharma Sep 13 '19 at 08:47
  • @RichardMcFriendOluwamuyiwa yes both fragments are in same activity ... but both opens different activity's on landscape mode ... so how can i track which fragment is user currently on ... and open other activity accordingly .. – Wahab Khan Jadon Sep 13 '19 at 10:31
  • @GovindSharma this solution not working for me ... "setUserVisibleHint works with view pager but not with regular fragment container. " this is the comment in that answer ... and i am working with regular fragment. – Wahab Khan Jadon Sep 13 '19 at 12:00
  • @bhavik-nathani's solution will let you know if a fragment is a `View`/`ViewGroup` is visible to a user, but I will advice you use the `FragmentManager` to do this. How to do this will now depend on which `FragmentManager` you are using and whether you are using `fragment transactions` or not. –  Sep 13 '19 at 12:00

1 Answers1

0

Try this method to solve your issue.

@Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            // Your code here
        } else {
            // Your code here
        }
    }
Bhavik Nathani
  • 470
  • 5
  • 13