Skip to content

Commit

Permalink
Remove fragment element from layouts
Browse files Browse the repository at this point in the history
This removes <fragment> tags from XML layouts in favor of programmatically attaching them. This fixes an issue where child fragments may not be inflated correctly if their layout depends on layout params from the container view.
  • Loading branch information
andrewbailey committed Jun 11, 2018
1 parent 8c00340 commit 4c3985b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.marverenic.music.databinding.ActivityLibraryBaseWrapperBinding;
import com.marverenic.music.player.PlayerController;
import com.marverenic.music.ui.BaseLibraryActivityViewModel.OnBottomSheetStateChangeListener.BottomSheetState;
import com.marverenic.music.ui.nowplaying.MiniplayerFragment;
import com.marverenic.music.ui.nowplaying.NowPlayingFragment;

import javax.inject.Inject;

Expand All @@ -28,7 +30,7 @@ public abstract class BaseLibraryActivity extends SingleFragmentActivity {
private BaseLibraryActivityViewModel mViewModel;

@Override
public void onCreate(Bundle savedInstanceState) {
public void onCreate(@Nullable Bundle savedInstanceState) {
JockeyApplication.getComponent(this).injectBaseLibraryActivity(this);
super.onCreate(savedInstanceState);

Expand All @@ -37,6 +39,16 @@ public void onCreate(Bundle savedInstanceState) {
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.miniplayer_container, MiniplayerFragment.newInstance())
.commit();

getSupportFragmentManager().beginTransaction()
.add(R.id.now_playing_container, NowPlayingFragment.newInstance())
.commit();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class MiniplayerFragment extends BaseFragment {
@Inject PlayerController mPlayerController;
@Inject ThemeStore mThemeStore;

public static MiniplayerFragment newInstance() {
return new MiniplayerFragment();
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public class NowPlayingFragment extends BaseFragment implements Toolbar.OnMenuIt

private Subscription mSleepTimerSubscription;

public static NowPlayingFragment newInstance() {
return new NowPlayingFragment();
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
24 changes: 4 additions & 20 deletions app/src/main/res/layout-land/activity_library_base_wrapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<com.marverenic.music.view.NestedCoordinatorLayout
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_height="match_parent"
android:layout_width="match_parent">

Expand Down Expand Up @@ -40,18 +39,11 @@
app:behavior_bottomSheetCallback="@{viewModel.bottomSheetCallback}">

<FrameLayout
android:id="@+id/now_playing_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:toolbar_marginTop="@{viewModel.statusBarHeight}"
android:visibility="@{viewModel.nowPlayingContentVisibility}">

<fragment
android:tag="BaseLibraryActivityWrapper.now_playing_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.marverenic.music.ui.nowplaying.NowPlayingFragment"/>

</FrameLayout>
android:visibility="@{viewModel.nowPlayingContentVisibility}"/>

<!-- Fake status bar -->
<View
Expand All @@ -61,22 +53,14 @@
android:background="?attr/colorPrimaryDark"/>

<FrameLayout
android:id="@+id/miniplayer_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:background="@color/background_miniplayer"
android:alpha="@{viewModel.miniplayerAlpha}"
android:visibility="@{viewModel.miniplayerVisibility}"
android:onClick="@{viewModel.miniplayerClickListener}">

<fragment
android:id="@+id/miniplayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.marverenic.music.ui.nowplaying.MiniplayerFragment"
tools:layout="@layout/fragment_miniplayer"/>

</FrameLayout>
android:onClick="@{viewModel.miniplayerClickListener}"/>

</FrameLayout>

Expand Down
24 changes: 4 additions & 20 deletions app/src/main/res/layout/activity_library_base_wrapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<com.marverenic.music.view.NestedCoordinatorLayout
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_height="match_parent"
android:layout_width="match_parent">

Expand Down Expand Up @@ -41,37 +40,22 @@
app:behavior_bottomSheetCallback="@{viewModel.bottomSheetCallback}">

<FrameLayout
android:id="@+id/now_playing_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:toolbar_alpha="@{viewModel.toolbarAlpha}"
app:toolbar_expanded="@{viewModel.toolbarExpanded}"
android:visibility="@{viewModel.nowPlayingContentVisibility}">

<fragment
android:tag="BaseLibraryActivityWrapper.now_playing_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.marverenic.music.ui.nowplaying.NowPlayingFragment"/>

</FrameLayout>
android:visibility="@{viewModel.nowPlayingContentVisibility}"/>

<FrameLayout
android:id="@+id/miniplayer_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:background="@color/background_miniplayer"
android:alpha="@{viewModel.miniplayerAlpha}"
android:visibility="@{viewModel.miniplayerVisibility}"
android:onClick="@{viewModel.miniplayerClickListener}">

<fragment
android:id="@+id/miniplayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.marverenic.music.ui.nowplaying.MiniplayerFragment"
tools:layout="@layout/fragment_miniplayer"/>

</FrameLayout>
android:onClick="@{viewModel.miniplayerClickListener}"/>

</FrameLayout>

Expand Down

0 comments on commit 4c3985b

Please sign in to comment.