Search code examples
javaandroidandroid-fragmentsandroid-activitytextfield

Strange situation: text from MainActivity being shown in every fragment


I'm new here and I'm writing this question, because I'm making my first application for Android devices. I use Android Studio, of course. My problem is:

I have got a strange situation: I have a TextVield with a kind of welcome to the user in MainActivity, which is shown when the app starts and when I'm using the hamburger menu to navigate to other fragments in my app, the text from MainActivity is visible in every fragment. Hamburger itself works right. What can I do to fix this issue?

Here are parts of my code - I think the crucial ones, but I'm not 100% sure, because I'm quite new to Android and Java.

This comes from MainActivity.java:

public void selectedtMenuItem(MenuItem menuItem)
{
    android.support.v4.app.Fragment mfragment= null;
    Class fragmentClass;

    switch (menuItem.getItemId())
    {
        case R.id.main:
            fragmentClass = main.class;    
            break;
        case R.id.menu:
            fragmentClass = menu.class;    
            break;
        case R.id.galery:
            fragmentClass = galery.class;  
            break;
        case R.id.contact:
            fragmentClass = contact.class;  
            break;
        default:
            fragmentClass = main.class;   
    }
    try
    {
        mfragment= (android.support.v4.app.Fragment) fragmentClass.newInstance();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                                                                            
    fragmentManager.beginTransaction().replace(R.id.fragmentContent, mfragment).addToBackStack(null).commit();
    menuItem.setChecked(true);
    setTitle(menuItem.getTitle());
    mlayout.closeDrawers();

}

private void setContent (NavigationView navigationView)
{
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
    {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item)
        {
            selectedtMenuItem(item);
            return false;
        }
    });
}

This comes from activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/main_activity"
    tools:context=".MainActivity">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragmentContent" >

        <TextView
            android:id="@+id/welcome"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
         />

    </android.support.constraint.ConstraintLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nawigacja"
        app:headerLayout="@layout/header"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/white"
        app:itemTextColor="@color/black"
        app:itemIconTint="#303F9F"
        app:menu="@menu/drawermenu"
        android:layout_gravity="start">
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

And perhaps AndroidManifest.xml would be useful:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.menu_boczne">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/logo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Solution

  • It's fairly simple: what you are doing is adding fragment to the ConstraintLayout that you have. It does not remove TextView you have, rather just adds fragment's UI elements after it.

    What you should do to retain your design:

    1. Add another element, FrameLayout will do, after that TextBox and move your id fragmentContent to it from your ConstraintLayout

      <android.support.constraint.ConstraintLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <TextView
              android:id="@+id/welcome"
              android:layout_width="300dp"
              android:layout_height="wrap_content"
              android:layout_centerInParent="true"
              android:layout_gravity="center"
              android:layout_marginBottom="8dp"
              android:layout_marginEnd="8dp"
              android:layout_marginStart="8dp"
              android:layout_marginTop="8dp" />
      
           <FrameLayout
              android:id="@+id/fragmentContent"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />
      
      </android.support.constraint.ConstraintLayout>
      
    2. Make transaction towards it, just like you are already doing:

      fragmentManager.beginTransaction().replace(R.id.fragmentContent, mfragment).addToBackStack(null).commit();