Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this activiy finish?

Tags:

java

android

I have following code:

    mQuestions=DictionaryDbWrapper.getInstance().getQuestionsSequence(
            this.getIntent().getStringExtra(ApplicationUtilities.TEST_CATEGORY_PARAMETER), 50);
    mQuestionsCount=mQuestions.size();
    Log.e("count", String.valueOf(mQuestionsCount));
    if (mQuestionsCount==0) {
        Log.e("1", "111");
        Toast.makeText(this, "В данной категории нет слов", Toast.LENGTH_LONG).show();
        this.finish();
    }

    makeQuestion();

mQuestions is empty ArrayList, and I see that count equals 0 on Log always. Also I see 1/111 record on my Log always too. But my activity doesn't do a finish method!

makeQuestion is method that needn't work with empty mQuestion (it throw Exception). But If I make a comment for makeQuestion then finish method works well! Method for this code works on main thread (it is executed from onCreate() method).

Please, suggest where I am going wrong.

Thanks in advance.

like image 200
user1166635 Avatar asked Jan 28 '12 15:01

user1166635


2 Answers

The finish() function is non-blocking, this means that the execution will continue while the activity is finishing in background.

To solve your problem, add a return statement after the finish() call.

like image 151
Alexei Avatar answered Oct 08 '22 01:10

Alexei


I am pretty sure that calling finish() does not have the similar blocking effect of calling return.

Based on your code, you are assuming that finish() will have the same blocking effect of return.

Try this:

mQuestions=DictionaryDbWrapper.getInstance().getQuestionsSequence(
        this.getIntent().getStringExtra(ApplicationUtilities.TEST_CATEGORY_PARAMETER), 50);
mQuestionsCount=mQuestions.size();
Log.e("count", String.valueOf(mQuestionsCount));
if (mQuestionsCount==0) {
    Log.e("1", "111");
    Toast.makeText(this, "В данной категории нет слов", Toast.LENGTH_LONG).show();
    this.finish();
}
else {
    makeQuestion();
}
like image 37
Jonathan Avatar answered Oct 08 '22 02:10

Jonathan