Title

Monday, 19 January 2015

Android EditText and OnEditorActionListener - memory leak


I am tracking down a memory leak in my Android app and this EditText uses a lot of memory (2.8MB) and disabling it makes the leak disappear.

So can anyone spot a (potential) memoryleak in this code? I find it a bit problematic that I never return true from the OnEditorActionListener since I replace the fragment before (or will the return still be called? I should step through the code and check that).

Also, maybe I am not closing the editor properly, just hiding it away?

 public void addHighscoreEdittext()  {   final HighscoresDatabaseHandler dbHandler = ( (MainActivity) getActivity() ).theDatabasehandler;     HighscoreEdittext = new EditText( getActivity() );   HighscoreEdittext.setX( ( actualScreenSizeX - actualScreenSizeX / 8 * 6 ) / 2 );   HighscoreEdittext.setY( actualScreenSizeY / 10 * 3 );     LayoutParams lparams = new LayoutParams( actualScreenSizeX / 8 * 6, actualScreenSizeY / 6 );   HighscoreEdittext.setLayoutParams( lparams );   HighscoreEdittext.setInputType( InputType.TYPE_CLASS_TEXT );     HighscoreEdittext.setBackground( getResources().getDrawable( R.layout.start_finish_button ) );     HighscoreEdittext.setTextColor( Constants.TEXT_COLOR_ON_TRANSPARENT_BROWN );   HighscoreEdittext.setHintTextColor( Constants.TEXT_COLOR_ON_TRANSPARENT_BROWN );   HighscoreEdittext.setHint( "New best time! Enter name..." );     ((ViewGroup) (getActivity()).findViewById( android.R.id.content )).addView( HighscoreEdittext );     HighscoreEdittext.setOnEditorActionListener(   new EditText.OnEditorActionListener()   {   @Override   public boolean onEditorAction( TextView v, int actionId, KeyEvent event )   {   if ( actionId == EditorInfo.IME_ACTION_DONE )   {   SharedData.GameFinished = false;     String name = v.getText().toString();   if ( name.length() > 8 )   {   name = name.substring( 0, 8 );   }   dbHandler.updateHighscore(   new Highscore(   SharedData.LevelPlayed,   name,   SharedData.LevelTime ) );     /* Hide keyboard */   HighscoreEdittext.clearFocus();   InputMethodManager in = (InputMethodManager) getActivity().getSystemService( Context.INPUT_METHOD_SERVICE );   in.hideSoftInputFromWindow( HighscoreEdittext.getApplicationWindowToken(),   InputMethodManager.HIDE_NOT_ALWAYS );     ((ViewGroup) getActivity().findViewById( android.R.id.content )).removeView( HighscoreEdittext );     FragmentManager fragmentManager = getActivity().getSupportFragmentManager();   FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();   fragmentTransaction.replace( R.id.fragment_content, new ChooseLevelFragment(), "chooselevel" );   fragmentTransaction.commit();     return true;   }     return false;   }   });   }
Answer

Where you have fragmentTransaction.commit(). This may be the issue since commit is a synchronous task and will cause the app to wait for it. See if you can use an async task for it if you do not need the data right away.

Answer

Where you have fragmentTransaction.commit(). This may be the issue since commit is a synchronous task and will cause the app to wait for it. See if you can use an async task for it if you do not need the data right away.

No comments:

Post a Comment