Android - Dynamic Fragment problems on orientation change

后端 未结 1 906
无人共我
无人共我 2021-01-25 10:32

I\'m having a problem with dynamic fragment . If I\'m not change orientation , it work fine . When I change orientation , I click on ListView item . It\'s not change textview .<

相关标签:
1条回答
  • 2021-01-25 11:03

    I found the problems are : When the system destroys and re-creates an activity because of a run-time configuration change, the activity automatically re-instantiates existing fragments.

    This isn’t a problem for “static” fragments declared in the activity’s layout.

    But for “dynamic” fragments, i need to test for this situation to prevent creating a second instance of my fragment.

    I check the Bundle argument passed to my activity’s onCreate() is null.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic);
    
        if(savedInstanceState == null)
        {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            ListContentFragment listContentFragment = new ListContentFragment();
            DetailsContentFragment detailsContentFragment = new DetailsContentFragment();
            transaction.add(R.id.listContainer, listContentFragment,"listContent");
            transaction.add(R.id.detailsContainer, detailsContentFragment,"detailsContent");
            transaction.commit();
        }
    
    }
    

    And it work fine . I think is helpful for someone have same problems .

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