3

I need some help with a problem I really don't undestand.

I'm working on my app which I debugg with real devices. I have two, the first one is a motorola with really low resources (Android 5.0.2) which helps me to catch more errors; in this one the app strangly works perfectly. The second device is a Nexus 6 (Android 6.0.1) where the app works really bad, it goes really slow (even withouth doing anything, for example opening a menu) and it shows me a message like this: Skipped 33 frames! The application may be doing too much work on its main thread.

I am using asynk tasks when I have to, and I use main thread for the less things I can. As I said before it shows the "skkiped frames" message even if I only open a menu (it means that I am not downloading or doing strange things). The app never crashes but it goes extremly slow.

I have installed everything on my SDK manager.

I am going to explain you an example where my app goes slow and show you the code. In my main activity I have a Toolbar with a NavigationView, so when I click the "Menu button" to open it, it takes like 3 seconds to open it.

MainActivity code:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {

private Toolbar toolbar;
//private FloatingActionButton fab;
private DrawerLayout drawer;
private ActionBarDrawerToggle toggle;
private NavigationView navigationView;
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
private static final String TAG = "MainActivity";

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

    initMaterialDesign();

    if (savedInstanceState == null){
        showFragment(Constants.FRAGMENT_MENU);
        navigationView.getMenu().getItem(0).setChecked(true);
        getSupportActionBar().setTitle("Menú");
    } else{

    }


}

private void initMaterialDesign(){
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    /*fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(this);*/

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

    toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

    drawer.addDrawerListener(toggle);

    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

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

    if (id == R.id.nav_menu) {
        getSupportActionBar().setTitle("Menú");
        showFragment(Constants.FRAGMENT_VALORES);
    } /*else if (id == R.id.nav_gallery) {
        item.setChecked(true);
    } */else if (id == R.id.nav_contactanos) {
        contactanos();
    } /*else if (id == R.id.nav_manage) {
        item.setChecked(true);
    }*/else if (id == R.id.nav_logut) {
        logOut();
    }
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onClick(View v) {
    switch (v.getId()){
       /* case R.id.fab:
            Snackbar.make(v, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
            break;*/

    }
}

private void showFragment(int fragment){
    fragmentManager = getSupportFragmentManager();
    fragmentTransaction = fragmentManager.beginTransaction();
    switch (fragment){
        /*case Constants.FRAGMENT_VALORES:
            ValoresFragment valoresFragment = new ValoresFragment();
            fragmentTransaction.replace(R.id.main_act_contenedorFragments, valoresFragment);
            break;
        case Constants.FRAGMENT_NOTICIAS:
            break;
        case Constants.FRAGMENT_WATCHLIST:
            break;
        case Constants.FRAGMENT_PORTFOLIO:
            break;*/
        case Constants.FRAGMENT_MENU:
            MenuFragment menuFragment = new MenuFragment();
            fragmentTransaction.replace(R.id.main_act_contenedorFragments, menuFragment);
            break;
    }
    fragmentTransaction.commit();
}

private void logOut(){
    new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
            .setTitleText(getString(R.string.nav_logout))
            .setContentText(getString(R.string.seguro_logout))
            .showCancelButton(true)
            .setConfirmText(getString(R.string.aceptar))
            .setCancelText(getString(R.string.cancelar))
            .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                @Override
                public void onClick(SweetAlertDialog sDialog) {
                    sDialog.dismiss();
                }
            })
            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                @Override
                public void onClick(SweetAlertDialog sDialog) {
                    sDialog.dismissWithAnimation();
                    borrarSPUser();
                    irLoginAct();
                    MainActivity.this.finish();

                }
            })
            .show();
}

private void borrarSPUser(){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("usuario", "");
    editor.putString("pass", "");
    editor.commit();
}

private void irLoginAct(){
    Intent intentRegistroActivity = new Intent(MainActivity.this, LoginActivity.class);
    startActivity(intentRegistroActivity);
}

private void contactanos(){

    final Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"comunica@gmail.com"});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My App");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Cuéntanos lo que quieras sobre la bolsa");

    startActivity(Intent.createChooser(emailIntent, "Elegir cliente:"));
}
}

I know this code won't help you for getting an answer but I just want to show you that I am not doing anything strange.

This is the logcat:

    04-14 12:34:28.596 8419-8419/? I/art: Late-enabling -Xcheck:jni
    04-14 12:34:28.646 8419-8419/? W/System: ClassLoader referenced unknown path: /data/app/com.myapp-1/lib/arm
    04-14 12:34:28.677 8419-8419/? V/Aplicacion: initParse: Parse iniciado
    04-14 12:34:28.725 8419-8425/? I/art: Debugger is no longer active
    04-14 12:34:28.764 8419-8445/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
    04-14 12:34:28.815 8419-8445/? I/Adreno: QUALCOMM build                   : 52af4d2, I8366cd0437
                                             Build Date                       : 10/20/15
                                             OpenGL ES Shader Compiler Version: XE031.05.13.02
                                             Local Branch                     : M14
                                             Remote Branch                    : 
                                             Remote Branch                    : 
                                             Reconstruct Branch               : 
    04-14 12:34:28.819 8419-8445/? I/OpenGLRenderer: Initialized EGL, version 1.4
    04-14 12:34:31.008 8419-8419/com.myapp V/LoadingActivity: DescargarAcciones >> Lista Acciones guardada en Application class
    04-14 12:34:32.369 8419-8419/com.myapp V/LoadingActivity: getLastUpdate >> UPDATE
    04-14 12:34:33.216 8419-8425/com.myapp W/art: Suspending all threads took: 28.414ms
    04-14 12:34:33.663 8419-8419/com.myapp V/LoadingActivity: descargarHistorial >> historialIbex35 descargado y guardado
    04-14 12:34:33.664 8419-8419/com.myapp V/LoadingActivity: modificarUltimoUpdate - Void done >> update realizado
    04-14 12:34:34.000 8419-8419/com.myapp V/LoadingActivity: descargarRanking >> EXITO
    04-14 12:34:34.000 8419-8419/com.myapp V/LoadingActivity: parsearRanking >> listaRanking guardada correctamente
    04-14 12:34:34.211 8419-8425/com.myapp W/art: Suspending all threads took: 22.491ms
    04-14 12:34:34.915 8419-8419/com.myapp I/Choreographer: Skipped 32 frames!  The application may be doing too much work on its main thread.
    04-14 12:48:33.438 8419-8419/com.myapp I/Choreographer: Skipped 35 frames!  The application may be doing too much work on its main thread.
    04-14 12:48:42.979 8419-8419/com.myapp I/Choreographer: Skipped 32 frames!  The application may be doing too much work on its main thread.
    04-14 12:48:43.537 8419-8419/com.myapp I/Choreographer: Skipped 33 frames!  The application may be doing too much work on its main thread.

I really don't undestand what is going on. I would like to remember that this is working perfectly on a low resources device and it is going really slow on the good one. I have serached on internet but I have not found anything. If someone could give me a hint, it would be really appreciated. Thanxs in advance.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
QuinDa
  • 918
  • 1
  • 12
  • 19
  • Those are my log messages. MainActivity is not showed up until everything is finished in LoadingActivity. I use Parse.com and AsyncTask. I am not doing network IO on main thread. Shall I add my LoadingActivity to the post?....(this was an answer for a deleted question) – QuinDa Apr 14 '16 at 11:36

1 Answers1

3

After so long looking for an answer to my question, I did not find anything to solve it but I got some hints that helped me a lot to get the answer.

I found some answers which suggested that the problem could be caused by the resources (drawables) but I was sure my drawables were properly resized for every screen density my app was made for.

The problem was that I wrongly assumed Nexus 6 (and every device which is not a tablet) screen density was contemplanted from ldpi to xxhdpi. In this case, Nexus 6 screen density is DisplayMetrics.DENSITY_560, so as we can find here it is an

intermediate density for screens that sit somewhere between DENSITY_XXHIGH (480 dpi) and DENSITY_XXXHIGH (640 dpi).
So the documentation says:
This is not a density that applications should target, instead relying on the system to scale their DENSITY_XXXHIGH assets for them.

In conclusion, the problem was caused when the backgorund was tried to be scaled for Nexus 6 screen density so the application was entering an endless loop.

Finally I created the xxxhdpi-drawable folder and the problem was solved.

It was pretty difficult to detect this for me because I had the same background for the whole app, so it was working wrong in every activity since I launched the app, there was not an activity with no background or a different background which could work right.

I hope this can help someone who gets the same problem.

QuinDa
  • 918
  • 1
  • 12
  • 19