Yes, I've already read the article...

This is th code for the onPause and onResume methods.

        public void onResume(){
                super.onResume();
                if(!mWakeLock.isHeld()){
                        pm = 
(PowerManager)getSystemService(Context.POWER_SERVICE);
                        mWakeLock =
pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,getClass().getName());
                        mWakeLock.acquire();
                }
                System.gc();
                System.runFinalization();
                System.gc();
                if(Sound.soundEnabled){
                        mNowPlaying.start();
                }
        }

        @Override
        public void onPause(){
                super.onPause();
                System.gc();
                System.runFinalization();
                System.gc();
                mWakeLock.release();
                if(Sound.soundEnabled){
                        mNowPlaying=Sound.getNowPlaying();
                        Sound.stopSound(mNowPlaying);
                }
                if (Profile.getActiveProfileIndex() != -1) {
                        Profile.getActiveProfile().saveData();
                }
        }

public void saveData() {
//              Log.d("", "saving data!");
                        save();
                        saveActiveFile();
        }

private void save() {
                FileOutputStream fos = null;
                DataOutputStream dos = null;
                File file = new File(Main.instance.getFilesDir() + "/" +
Main.instance.getString(R.string.T_APPNAME) + getActiveProfileIndex()
+1);
                try {
                        if(!file.exists()){
                                file.createNewFile();
                        }
                        fos =
Main.instance.openFileOutput(Main.instance.getString(R.string.T_APPNAME)
+ this.mProfileId, Context.MODE_PRIVATE);
                        dos = new DataOutputStream(fos);
                        int score = 0;
                        int status = 0;
                        int tries = 0;
                        for (int i = 0; i < MAX_LEVEL_NUMBER; i++) {
                                dos.writeInt(mLevels[i].getStatus().ordinal());
                                dos.writeInt(mLevels[i].getScore());
                                dos.writeInt(mLevels[i].getTries());
//                              mLevels[i] = new ProfileLevel(i, status, score, 
tries);
                        }
                        dos.writeInt(this.getLastPageViewed());
                        dos.writeUTF(this.getName());
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        try {
                                dos.flush();
                                dos.close();
                                dos = null;
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                }
//              Log.d("", "file saved " + this.mProfileId);
                for (FilesId fId : FilesId.values()) {
                        if (fId.ordinal() > 0 && 
Main.mProfiles[fId.ordinal()-1]==null) {
                                file = new File(Main.instance.getFilesDir() + 
"/" +
Main.instance.getString(R.string.T_APPNAME) + fId.ordinal());
                                if (file.exists()){
//                                      Log.d("", "deleting file " + 
(fId.ordinal()-1));
                                        file.delete();
                                }
                        }
                }
        }


private static void saveActiveFile() {
                FileOutputStream fos = null;
                DataOutputStream dos = null;
//              File file = new File(Main.instance.getFilesDir() + "/" +
Main.instance.getString(R.string.T_APPNAME) + FilesId.ACTIVE_PROFILE);
                try {
//                      file.createNewFile();
                        fos =
Main.instance.openFileOutput(Main.instance.getString(R.string.T_APPNAME)
+  FilesId.ACTIVE_PROFILE, Context.MODE_PRIVATE);
                        dos = new DataOutputStream(fos);
//                      Log.d("", "saving active profile file " +
Profile.getActiveProfileIndex() + " in " +
Main.instance.getString(R.string.T_APPNAME) +
FilesId.ACTIVE_PROFILE);
                        dos.writeInt(Profile.getActiveProfileIndex());
                        dos.flush();
                        dos.close();
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        try {
//                              dos.flush();
//                              dos.close();
//                              dos = null;
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                }
                FileInputStream fis=null;
                DataInputStream dis=null;
                try {
//                      Log.d("", "reading active profile file in " +
Main.instance.getString(R.string.T_APPNAME) +
FilesId.ACTIVE_PROFILE);
                        fis =
Main.instance.openFileInput(Main.instance.getString(R.string.T_APPNAME)
+ FilesId.ACTIVE_PROFILE);
                        dis=new DataInputStream(fis);
//                      Log.d("", "active profile before exit " + 
dis.readInt());
                } catch (Exception ex) {
                        ex.printStackTrace();
                }
                finally {
                        try {
                                dis.close();
                                dis = null;
                                fis.close();
                                fis = null;
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                }


        }

Hope it helps!


On Mar 17, 10:02 am, Yahel <[email protected]> wrote:
> Hi Luca,
>
> As Anton told you, withouth the code in the onpause and onresume, hard
> to tell if you're  doing something wrong.
>
> One option might be to save the state of your app in onpause and then
> restore that state in your onresume.
>
> This way you could clean up all the bitmaps in the onpause and
> recreate them in the onresume. Not very efficient but a sure-fire work-
> around :)
>
> I sure you already read Romain Guy's article but just to be sure 
> :http://www.curious-creature.org/2008/12/18/avoid-memory-leaks-on-andr...
>
> Yahel

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe from this group, send email to 
android-developers+unsubscribegooglegroups.com or reply to this email with the 
words "REMOVE ME" as the subject.

Reply via email to