Android - Dynamic Fragment problems on orientation

2019-02-28 06:37发布

问题:

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 .

This is DynamicActivity class

public class DynamicActivity extends Activity implements FragmentCoordinator{

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

    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();

}

@Override
public void onSetContentDetails(int index) {
    FragmentManager fragmentManager = getFragmentManager();

    DetailsContentFragment detailsContentFragment = (DetailsContentFragment)fragmentManager.findFragmentByTag("detailsContent");

    detailsContentFragment.setContentDetails(index);
}

}

And DetailsContentFragment class

public class DetailsContentFragment extends Fragment {

TextView lbMess;
String[] array;
int saveIndex;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.details_content_fragment, container,false);
    array = getResources().getStringArray(R.array.list_details);
    lbMess = (TextView)v.findViewById(R.id.lbDetails);

    int currentIndex = savedInstanceState == null ? 0 : savedInstanceState.getInt("indexContent",0);
    setContentDetails(currentIndex);

    return v;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putInt("indexContent", saveIndex);
}

public void setContentDetails(int index) {
    saveIndex = index;
    lbMess.setText(array[saveIndex]);
}
}

I have debug but it doesn't have any error . Please give me some advice

回答1:

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 .