In Fragment on back button pressed Activity is blank

前端 未结 11 1698
囚心锁ツ
囚心锁ツ 2020-11-29 20:04

I have an Activity and many fragments inflated in same FrameLayout



        
相关标签:
11条回答
  • 2020-11-29 20:36

    If you have more than one fragment been used in the activity or even if you have only one fragment then the first fragment should not have addToBackStack defined. Since this allows back navigation and prior to this fragment the empty activity layout will be displayed.

     // fragmentTransaction.addToBackStack() // dont include this for your first fragment.
    

    But for the other fragment you need to have this defined otherwise the back will not navigate to earlier screen (fragment) instead the application might shutdown.

    0 讨论(0)
  • 2020-11-29 20:36

    Sorry for the late response.

    You don't have to add ft.addToBackStack(null); while adding first fragment.

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.content_frame, profileFragment);
    // ft.addToBackStack(null); --remove this line.
    ft.commit();
    // ... rest of code
    
    0 讨论(0)
  • 2020-11-29 20:36

    Just don't add the first fragment to back stack

    Here is the Kotlin code that worked for me.

        val ft = supportFragmentManager.beginTransaction().replace(container, frag)
        if (!supportFragmentManager.fragments.isEmpty()) ft.addToBackStack(null)
        ft.commit()
    
    0 讨论(0)
  • 2020-11-29 20:36

    I still could not fix the issue through getBackStackEntryCount() and I solved my issue by making the main page a fragment too, so in the end I have an activity with a FrameLayout only; and all other fragments including the main page I inflate into that layout. This solved my issue.

    0 讨论(0)
  • 2020-11-29 20:41

    I had the same problem when dealing with Firebase's Ui Login screen. When back button was pressed it left a blank screen.

    To solve the problem I just called finish() in my onStop() method for said Activity. Worked like a charm.

    0 讨论(0)
提交回复
热议问题