Search code examples
javaandroidandroid-fragmentsfragmentandroid-5.0-lollipop

Activity is hidden by fragment, it is not useable


i have builded an app that works perfectly on android with api higer than 21. The problem is that this instruction of my code:

        mFragmentTransaction.replace(R.id.content_frame, new TabFragment()).commit();

Works in different way on API less then 21.

On Api less then 21 the new fragment hide the previus activity, so that i can't click on my Floating Action Button. Here are two images that explain in abetter way my problem.

API HIGER THAN 21

enter image description here

API LESS THAN 21

enter image description here

So my question is: How can i have the same result in API less then 21 that i have on API Higer then 21?

Here is the affected part of the Main Activity Code:

public class MainActivity extends AppCompatActivity {
public static AppDataBase appDataBase;
public static UserDataBase userDataBase;
static FragmentManager mFragmentManager;
static FragmentTransaction mFragmentTransaction;
private DrawerLayout myDrawerLayout;
final String TXT_MAINACTVT_USER_HAVE_NOT_ADDED_CONSOLE = "Add a console!";
TextView currentConsole;

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

    appDataBase = new AppDataBase(this);
    userDataBase = new UserDataBase(this);
    myDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    currentConsole = (TextView) findViewById(R.id.txt_Mainactvt_currentConsole);
    currentConsole.setText(TXT_MAINACTVT_USER_HAVE_NOT_ADDED_CONSOLE);

    tabLayoutManagement();
    floatingActionButtonManagement();
    leftDrawerMenuManagement();
    rigthDrawerMenuManagement();
    populateMyConsole();
}

void tabLayoutManagement() {
    mFragmentManager = getSupportFragmentManager();
    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.content_frame, new TabFragment()).commit();
}

// Floating Action Button
private void floatingActionButtonManagement() {
    FloatingActionButton fab_addGame = (FloatingActionButton)findViewById(R.id.fab_AddGame);
    fab_addGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*
            The currentConsole TextView is used to show at the user wich console is selected.
            We use it to have a strng that conteins the selected console.
            Call the method that manage the click event of the FloatingActionButton. We pass the console name.
             */
            String currentConsoleName = currentConsole.getText().toString();

            floatingActionButtonClickEvent(currentConsoleName);
        }
    });
}

private void floatingActionButtonClickEvent(String currentConsoleName) {
    /*
    Check if user have added a console. If he did start a menu for adding games, else start an
    error message
     */
    if (!currentConsoleName.equals(TXT_MAINACTVT_USER_HAVE_NOT_ADDED_CONSOLE)) {
        popUpViewAddGameBuild(currentConsoleName);
    }
    else
        mySimpleAlertDialogMethod("Attention!", "Before you enter game, you must enter a console.", true, true);
}

private void popUpViewAddGameBuild(String currentConsoleName) {
    /*
    Build the view that show the menu for adding games.
     */
    LayoutInflater inflater = this.getLayoutInflater();
    View popupView = inflater.inflate(R.layout.popupview_addgame, null);
    PopupWindow popupWindow = new PopupWindow(
            popupView,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT);
    popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
    popupWindow.setFocusable(true);
    popupWindow.showAtLocation(popupView, 0, 0, 0);
  }

Here is the TabLayout Class:

public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View x =  inflater.inflate(R.layout.tab_layout, null);
    tabLayout = (TabLayout) x.findViewById(R.id.tabs);
    viewPager = (ViewPager) x.findViewById(R.id.viewpager);

    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));



    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });
    return x;
}

public class MyAdapter extends FragmentPagerAdapter {
    public int position;

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0 :
                return new DesireFragment();
            case 1 :
                return new BuyedFragment();
            case 2 :
                return new StartedFragment();
            case 3 :
                return new FinishedFragment();
            case 4 :
                return new AllTrophiesFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 5;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case 0 :
                return "Desire";
            case 1 :
                return "Buyed";
            case 2 :
                return "Started";
            case 3 :
                return "Finished";
            case 4 :
                return "AllTrophies";
        }
        return null;
    }
}
}

There is the layout of the MainActivity:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="end"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:id="@+id/toolbar"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:navigationIcon="@drawable/ic_menu_white_24dp"
        app:title="MyGames">

        <Button
            android:id="@+id/btnOpenRigthDrawer"
            android:background="@drawable/ic_filter_list_white_24dp"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginRight="10dp"
            android:layout_gravity="right" />

        <Button
            android:id="@+id/btnOpenOptions"
            android:background="@drawable/ic_settings_white_24dp"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginRight="17dp"
            android:layout_gravity="right" />

    </android.support.v7.widget.Toolbar>

</RelativeLayout>

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollIndicators="bottom">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:backgroundTint="@color/colorPrimary"
        app:borderWidth="0dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/txt_Mainactvt_currentConsole"
            android:layout_gravity="center_horizontal|top"
            android:layout_marginTop="50dp"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_AddGame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="17dp"
            android:layout_marginRight="17dp"
            android:src="@drawable/ic_mode_edit_white_24dp"
            android:layout_gravity="bottom|right"
            android:background="@color/colorPrimary" />

    </FrameLayout>

    <include
        layout="@layout/drawer_left"
        android:id="@+id/layLeft"
        android:layout_gravity="start"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"/>

    <include
        layout="@layout/drawer_rigth"
        android:id="@+id/layRigth"
        android:layout_gravity="end"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:scrollbars="vertical"
        />
</android.support.v4.widget.DrawerLayout>

And here is the layout code of the TabLayout:

<?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"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">


<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    app:tabGravity="fill"
    app:tabMode="scrollable"
    android:background="@color/colorPrimary"
    app:tabIndicatorColor="@android:color/holo_orange_dark"
    app:tabSelectedTextColor="@android:color/holo_orange_dark"
    app:tabTextColor="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
</android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v4.view.ViewPager>


Solution

  • You replace the content of your FrameLayout with an Fragment. this leads to your strange result. Add a Layout to your Framelayout instead and use it as your FragmentContainer:

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:backgroundTint="@color/colorPrimary"
        app:borderWidth="0dp">
    
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:id="@+id/content_frame"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/txt_Mainactvt_currentConsole"
            android:layout_gravity="center_horizontal|top"
            android:layout_marginTop="50dp"/>
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_AddGame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="17dp"
            android:layout_marginRight="17dp"
            android:src="@drawable/ic_mode_edit_white_24dp"
            android:layout_gravity="bottom|right"
            android:background="@color/colorPrimary" />
    
    </FrameLayout>