开发者

Find Activity Position In Activity Stack

Basiclly I have two screens at the moment. Question and Answer screens. When you click on the answer you think it is on the question screen it takes you to the answer screen to tell you if you were correct. The next part is from that screen you go back to the question screen and you are asked another question. At the moment this is creating loads of new activities and can easily get out of hand. What I need is to know when a question activity gets 3 down and stop it. Basicly from the reason screen you can go back to view the question, but when you are on a new question 开发者_StackOverflowit should remove the old and the reason page.

How do I do this?


In the Answer Activity you should call this.finish(), and that will go back to the Question Activity. And in the Question Activity start the AnswerActivity with startActivityForResult, so that the question Activity will be notified when the Answer is finished, so you can show a new Question, etc. So each AnswerActivity will be destroyed. If you need help with this, post a code sample. If you architect this way you shouldn't need to find Activities in the stack.

Question Activity

this.startActivityForResult(Intent, requestCode); // somewhere in your code to launch the Answer Activity

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // now we have returned from the AnswerActivity and it has been destroyed
    // do any processing here on the answer or show a new question.
}

Answer Activity

public void onBackClick(View v) {
  //on click handler for a back button or something.
  this.setResult(resultCode, Intent /* some data to give back to parent */);
  this.finish();
}


I don't know of a way to do this directly from the framework, but you can do it manually (I think). You can maintain a list of integers at the Application level representing the task id's of the question activities. Each time an Activity starts, you push the value of getTaskId() onto this list. In the code that launches the Activity, you can broadcast an intent that the question activity responds to. If it's getTaskId value is in a certain position in your list, you can call finish() on the activity and remove its task id from the list.

This is a very hacky solution, but as far as I've experienced, singleTop launchMode just doesn't work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜