Search code examples
androidandroid-fragmentactivitybottomnavigationview

Fragment activity does not appear automatically


I'm working on a project in which I used Bottom Navigation Bar But when I run the application the fragment activity does not appear automatically until I tap on the icon in menu item of bottom navigation bar.

screenshot:

X

The blank activity appears first when I tap on any fragment activity then it appears.

Here is my code:

BottomNavigation.java

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;

public class BottomNavigation extends AppCompatActivity {

    private BottomNavigationView mMianNav;
    private FrameLayout mMainFrame;
    private AllResturant allResturant;
    private UserProfile userprofile;
    private ActivityUser activityUser;

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

        mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
        mMianNav = (BottomNavigationView) findViewById(R.id.main_nav);

        allResturant = new AllResturant();
        userprofile = new UserProfile();
        activityUser = new ActivityUser();

        //Listener for handling selection events on bottom navigation items
        mMianNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

                switch (menuItem.getItemId()) {

                    case R.id.toprofile:
                        setFragment(userprofile);
                        return true;

                    case R.id.tohome:
                        setFragment(allResturant);
                        return true;

                    case R.id.toactivity:
                        setFragment(activityUser);
                        return true;

                    case R.id.tologout:
                        logout();
                        return true;

                    default:
                        return false;
                }
            }

            private void logout() {
                Intent intent = new Intent(BottomNavigation.this, Registration.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
            }

        });
    }

    private void setFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.main_frame, fragment);
        fragmentTransaction.commit();

    }
}

BottomNavigation.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".BottomNavigation">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/main_nav"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        app:itemIconTint="@color/nav_items_color"
        app:itemTextColor="@color/nav_items_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:menu="@menu/nav_items" />

    <FrameLayout
        android:id="@+id/main_frame"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_above="@+id/main_nav"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        app:layout_constraintBottom_toTopOf="@+id/main_nav"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>
</android.support.constraint.ConstraintLayout>

How it will appear automatically?


Solution

  • modify your on create as below

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_navigation);
    
        mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
        mMianNav = (BottomNavigationView) findViewById(R.id.main_nav);
    
        allResturant = new AllResturant();
        userprofile = new UserProfile();
        activityUser = new ActivityUser();
    
           setFragment(userprofile);
    
        //Listener for handling selection events on bottom navigation items
        mMianNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    
                switch (menuItem.getItemId()) {
    
                    case R.id.toprofile:
                        setFragment(userprofile);
                        return true;
    
                    case R.id.tohome:
                        setFragment(allResturant);
                        return true;
    
                    case R.id.toactivity:
                        setFragment(activityUser);
                        return true;
    
                    case R.id.tologout:
                        logout();
                        return true;
    
                    default:
                        return false;
                }
            }
    
            private void logout() {
                Intent intent = new Intent(BottomNavigation.this, Registration.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
            }
    
        });
    }