By your post, I found that I have the exact problem as you.
The fragment will reload when the configuration change, so when the orientation change. Therefore you need to realod the view.
I used the post to find the solution.Change fragment layout on orientation change!
Here is the solution that works for me. Hope it will help you.
public class YourFragmentActivity extends Fragment {
private FrameLayout frameLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
frameLayout = new FrameLayout(getActivity());
LayoutInflater inflater2 = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
frameLayout.addView(ReloadView(inflater2));
return frameLayout;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
frameLayout. removeAllViews();
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
frameLayout .addView(ReloadView(inflater));
}
private View ReloadView(LayoutInflater inflater) {
View v = inflater.inflate(R.layout.your_layout, null);
//do your staff, button, listener, etc
return v;
}
}
And dont forget to add that in you manifest, otherwise it will not work.
<activity android:name="com.example.YourFragmentActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden"
android:configChanges="orientation|screenSize"/>