1
votes

Hi guys i'm trying to get separate activities by clicking menus from the navigation drawer, every thing seems fine but im getting

06-10 12:26:57.569 3457-3457/? I/Choreographer: Skipped 68 frames! The application may be doing too much work on its main thread.

error. and also app is very slow. please help me to solve the issue.

MainActivity.java

package gira.cdap.com.giira;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import gira.cdap.com.giira.activity.EventPlanningFragment;
import gira.cdap.com.giira.activity.HomeFragment;
import gira.cdap.com.giira.activity.LoginActivity;
import gira.cdap.com.giira.activity.ProfileFragment;
import gira.cdap.com.giira.activity.TourFragment;
import gira.cdap.com.giira.helper.SQLiteHandler;
import gira.cdap.com.giira.helper.SessionManager;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    NavigationView navigationview = null;
    Toolbar toolbar=null;


    private SQLiteHandler db;
    private SessionManager session;


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


        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //initNavigationDrawer();


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

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

        navigationview = (NavigationView) findViewById(R.id.navigation_view);

        // SqLite database handler
        db = new SQLiteHandler(getApplicationContext());

        // session manager
        session = new SessionManager(getApplicationContext());


        View headerView = navigationview.getHeaderView(0);

        navigationview.setNavigationItemSelectedListener(this);

        if (!session.isLoggedIn()) {
            logoutUser();
        }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.logout) {
            //Set the fragment initially
            logoutUser();
            Toast.makeText(getApplicationContext(), "Successfully Logout", Toast.LENGTH_SHORT).show();
        }
        else if (id ==R.id.home_menu){
            home_activity();
        }

        else if (id ==R.id.profile_menu){
            profile_activity();
        }

        else if (id ==R.id.tour_menu){
            tour_activity();
        }

        else if (id ==R.id.event_plan_menu){
            event_activity();
        }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private void logoutUser() {
        session.setLogin(false);

        db.deleteUsers();

        // Launching the login activity
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
    private void profile_activity(){
        Intent intent = new Intent(this, ProfileFragment.class);
        startActivity(intent);
        finish();
    }

    private void tour_activity(){
        Intent intent = new Intent(this, TourFragment.class);
        startActivity(intent);
        finish();
    }

    private void event_activity(){
        Intent intent = new Intent(this, EventPlanningFragment.class);
        startActivity(intent);
        finish();
    }
    private void home_activity(){
        Intent intent = new Intent(this, HomeFragment.class);
        startActivity(intent);
        finish();
    }

}

activity_main

<?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:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:fitsSystemWindows="true" tools:openDrawer="start">

    <include layout="@layout/app_bar_main" android:layout_width="match_parent"
        android:layout_height="match_parent" />


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



</android.support.v4.widget.DrawerLayout>
1
could you please explain in detail I'm new to Androidiam_muqshid

1 Answers

2
votes

that's because your launching your activity in onNavigationItemSelected during the menu closing. I suggest you to launch your activity after the menu is closed.

private static final int NAVDRAWER_ANIM_DURATION = 250;
public void goToActivity(Class<?> mClass){

    closeNavigationView(); // in this method you can close your drawer
    Intent intent = new Intent(this, mClass);
    new Handler().postDelayed(new ActivityTransitionRunnable(intent), NAVDRAWER_ANIM_DURATION);

}//goToActivity

private class ActivityTransitionRunnable implements Runnable {
    Intent mIntent;
    private ActivityTransitionRunnable(Intent intent) {
        mIntent = intent;
    }
    public void run() {
        startActivity(mIntent);
    }
}//ActivityTransitionRunnable