0

I'm trying to do navigation drawer with 2 fragment(later i will add), and 4 activities. In my code when i call activity and close. It's very laggy. I want it very smooth. Please see my code.

ublic class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    private DrawerLayout drawerLayout;
    private Toolbar toolbar;
    private NavigationView navigationView;
    FragmentManager fragmentManager;

    private String[] pageTitle;




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

        pageTitle = new String[]{getString(R.string.frag_home),
                getString(R.string.nav_introduction),};

        drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);

        toolbar = (Toolbar)findViewById(R.id.toolbar);
        toolbar.setTitle(pageTitle[0]);
        setSupportActionBar(toolbar);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
                R.string.drawer_open, R.string.drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();


        navigationView = (NavigationView)findViewById(R.id.nav_view);
//        assert navigationView != null: Log.wtf("haha", "OMG");
        assert navigationView != null;
        navigationView.setNavigationItemSelectedListener(this);


        fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.frame, new Fragment_home()).commit();
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {

        switch(item.getItemId()) {
            case R.id.nav_introduction: {

                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.frame, new Fragment_Introduction()).commit();
            }
                break;
            case R.id.nav_settings: {
                Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                startActivity(intent);
            } break;
            case R.id.nav_feedback: {
                Intent intent = new Intent(MainActivity.this, FeedbackActivity.class);
                startActivity(intent);
            }
        }

        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onBackPressed() {
        assert drawerLayout != null;
        if(drawerLayout.isDrawerOpen(GravityCompat.START)){
            drawerLayout.closeDrawer(GravityCompat.START);
        }
        else{
            super.onBackPressed();
        }
    }

    public void onClickHome(View view){
        fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.frame, new Fragment_home()).commit();
    }

    public void onClickFilter(View view){
        Intent intent = new Intent(MainActivity.this, FIlterActivity.class);
        startActivity(intent);
    }
}

And my main activity code is following:

<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:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:titleTextColor="@android:color/white" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:gravity="right">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="4">

            </LinearLayout>

            <TextView
                android:text=""
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:textColor="@android:color/white"
                android:gravity="right|center"
                android:layout_weight="1"
                android:onClick="onClickHome"/>

            <ImageView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginRight="3dp"
                android:gravity="center"
                android:src="@drawable/filter_icon"
                android:onClick="onClickFilter"
                />


        </LinearLayout>


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



    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/drawer_menu" />

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

</LinearLayout>

It's like when i use Activity and Fragment it's very laggy. Is there other way to do smooth?

Solution: I used handler. But it's not best. case R.id.nav_settings: {

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                    startActivity(intent);
                }
            }, 250);
        } break;
Greed Ruler
  • 169
  • 2
  • 14

0 Answers0