简体   繁体   中英

Android Activity lagging when starts

I have a navigation drawer and I want to open a new Activtiy.The navigation drawer and the MainAcitvity runs really smooth but when I want to start a new activtiy ist takes a lot of time and lagging

here is my code:

public void onNavigationDrawerItemSelected(int position) {
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, FirstFragment.newInstance(position + 1))
            .commit();

    if(position==5)
    {       Intent i=new Intent(MainActivity.this,SettingsActivity.class);
            startActivity(i);
            mTitle = getString(R.string.title_section1);
    }

}

I have no idea how solve this but is really annoying

That's a common problem and has nothing to do with the performance of your app, you can either delay the start of your new Activity with a Handler & Runnable (just experiment a bit with the delay, might be 100 or 200ms) or open your new Activity in OnDrawerClosed (eg set some boolean that the Activity needs to be started in onNavigationDrawerItemSelected ).

Also see this question.

There's nothing in your code to indicate that starting the new activity should take any more time than usual. Try setting StrictMode to ensure you aren't doing anything on the main UI thread you shouldn't be. Otherwise add some profiling code (basically timing log statements, or use a real profiler) to see where it's spending it's time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM