简体   繁体   中英

Android - After closing an activity, when I run the app again, two activities run at the same time. How can I avoid it?

So I've got an activity in my android app, that runs on start.
This activity is just a page with a start button.
When I press the start button, it calls another activity and closes itself:

Intent i = new Intent(this, Dictating.class);
startActivity(i);
finish();

The other Activity is using Text-to-speech to dictate some words.
Now I've got something weird happening:

1) I listen to the dictating.
2) I press back button: dictating stops (what I want)
3) I run again the app, press the start button. Now I have my new activity running and dictating, but in the back I can hear the older Activity that resumed where it was, and continues dictating.

I would like for the new activity to start all over again, and not keep the other activity.
How can I do that ?

PS: This is an activity problem, and not a text-to-speech problem as I'm flushing the text-to-speech each time, It could not be kept in the memory

Thank you

EDIT:

Here is the onCreate of my Dictating class, there is tons of code in this class, I obviously don't want to post all my code, so here is some parts:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.streaming);

    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

    this.txtCurrentWord = (TextView) findViewById(R.id.txtCurrentWord);
    this.btnPlayPause = findViewById(R.id.btnPlayPause);
    this.btnPlayPause.setOnClickListener(this);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // success, create the TTS instance
            this.tts = new TextToSpeech(this, this);
        } else {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }
}

there are a few weird things I'm doing like:

Runnable task = new Runnable() {
    public void run() {
        //runs on ui
        runOnUiThread(new Runnable() {
            public void run() {
                readNextWord();
            }
        });
    }
};
worker.schedule(task, 1, TimeUnit.SECONDS);

this delays the next word by one second, and then executes a fonction in the main ui thread. not sure if this matter

And some flushing at the end:

@Override
public void onDestroy() {
    tts.shutdown();
    super.onDestroy();
}

您需要在您的AndroidManifest文件中的活动中添加launchMode属性,有关更多详细信息,请参见“使用清单文件”

This question is over a year old, but I can't believe no one ever gave you the right answer. Also, I don't think you should have accepted an answer that clearly didn't solve anything for you. By accepting such answers, you're just cluttering the StackOverflow google search results with junk for other people with the same problem.

The flushing you do at the end is completely wrong. According to the Activity lifecycle, onDestroy() is never guaranteed to be called. If you want to make sure the flushing gets done properly, do it inside of onPause().

For now the solution I'm giving you does fix the main problem you've described. However, if you do get the time to do a more complete rewrite, you'll want use a service that you bind to your activity. That will give you the finer control you require.

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