简体   繁体   中英

Go Back takes a long time

In a child activity, I record some audio using MediaRecorder and have two buttons: The first one will start recording and second one is going back to its parent activity. However every time I hit go back button, it takes a long time to return to main activity's view. My code inside go back button's callback is:

public void onClick(View arg0) {        
        if (arg0.getId() == R.id.startRecord) 
        {
           StartRecording();
        } 

        else if (arg0.getId() == R.id.goBack) 
       {
       if(mediaRecorder!=null)
           {
             mediaRecorder.stop();
             mediaRecorder.release();
             mediaRecorder = null;
           }
       this.finish();

       }
   }

The parent activity's onCreate() method only initiate several buttons and set listener methods to them. I really cannot figure out why go back action will take a long time. A phenomenon worthy to mention is that if I don't record first but hit go back button first, it goes back really quick to the parent activity. The long responding time only happens after I record some audio. I do upload the recorded audio however I put the uploading in a AsyncTask task and I can get feedback when uploading work is done. After I see the feedback, and even wait for some time, the go back button still takes a long time to bring me back to main activity. Anyone has advices on this? Thanks!

You could override the back button, albeit from what I have seen it seems a lot of developers aren't to friendly to the idea.

@Override
public void onBackPressed() {
   mediaRecorder.stop();
   mediaRecorder.release();
   mediaRecorder.reset();
   mediaRecorder = null;
}

See if that works for you.


Better yet, something like this could be more useful, that way you could easily call the stop() method throughout with ease:

public void onClick(View arg0) {        
    if (arg0.getId() == R.id.startRecord) {
        StartRecording();
    } else if (arg0.getId() == R.id.goBack) {
        if(mediaRecorder!=null) {
        stopRecording();
    }
    this.finish();
}


public void stopRecording() throws IOException {
    mediaRecorder.stop();
    mediaRecorder.release();
    mediaRecorder.reset();
    mediaRecorder = null;
}

@Override
public void onBackPressed() {
   stopRecording();
}

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