My activity should handle portrait and landscape layouts in different ways: if layout is portrait, then in should use ViewPager
with two fragments. If layout is landscape, then it should just divide screen on two parts and locate two fragments there. The problem is that when I switch from portrait to landscape, viewpager's fragments are not removed and onCreateView()
is called for them.
Calling removing fragments with FragmentTransaction does nothing.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.weather_layout, new CurrentWeatherFragment(), "current");
fragmentTransaction.replace(R.id.forecast_layout, new ForecastFragment(), "forecast");
fragmentTransaction.commit();
}
else {
ViewPager viewPager = findViewById(R.id.pager);
WeatherPageAdapter weatherPageAdapter = new WeatherPageAdapter(getSupportFragmentManager());
viewPager.setAdapter(weatherPageAdapter);
viewPager.addOnPageChangeListener(new WeatherPageChangedListener());
viewPager.setCurrentItem(viewModel.getViewPagerPosition(), false);
}
}
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import com.example.android.myapplication.ui.fragments.CurrentWeatherFragment;
import com.example.android.myapplication.ui.fragments.ForecastFragment;
public class WeatherPageAdapter extends FragmentPagerAdapter {
private static final int pageNumber = 2;
public WeatherPageAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public int getCount() {
return pageNumber;
}
@Override
public Fragment getItem(int i) {
if (i == 0) {
return new CurrentWeatherFragment();
}
else {
return new ForecastFragment();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="7"
android:columnCount="2"
tools:context=".ui.activities.MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="7"
android:columnCount="2"
tools:context=".ui.activities.MainActivity"
android:orientation="horizontal"
android:baselineAligned="false">
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/weather_layout">
</FrameLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/forecast_layout">
</FrameLayout>
</LinearLayout>