简体   繁体   中英

Using finish() to “end” an activity manually: To or Not to?

Have a question for Android Developers. I've been in the Udacity Android Beginners course over the last couple of months and had a fellow student tell me I should be calling the finish() method on my intents when I move to a new Activity. I googled around and looked at developer documentation to try and discover where he learned about this method and what the documentation says about calling it but I can only find general explanations to how it works and not much in terms of when to call it and why to call it.

I understand what a stack is in memory and he mentioned that its to clear that stack but I've read that Android handles this on its own and we don't need to worry about it. I checked another question here on Stack Overflow which is similar to what I asked but doesn't quite answer the question: Do I need to explicitly call the finish() method on my intents or does Android handle this? If I do need to call finish() , what are the best practices for calling it? As a reference and in case my question is not clear for some reason here is some sample code from my app and a link to the GitHub repo :

Intent nextIntent= new Intent(OtherActivity.this, MainActivity.class); 
startActivity(nextIntent); 
finish(); 

If you call finish() , android takes your activity out of the activity stack. If you don't call it, the activity remains in the stack and if you press back button on your android phone, the default behavior is that the android will pop back the top activity on the stack. You can of course override the back button functionality. The most common use of calling finish on an activity is when you don't want to comeback to this activity, like for example if you have a splash screen activity. You would want to call finish on it when you move away from it. Other use cases totally depend on application specific navigation hierarchy and performance goals.

No. You don't have to call finish() on each of your Activity transitions as Android handles the stack properly. However, you might need the explicit finish() call in several places. For example,

  • While you are starting an Activity by calling startActivityForResult and from the Activity started, you want to pass the result back to the calling Activity using setResult . Then you might need finish() the Activity started after sending the result to the calling Activity in some cases.
  • When you are opening a DialogActivity , you might have some logic on which you want the DialogActivity to disappear. Then you call the finish() function explicitly.
  • A fullscreen Activity , might require disabling the onBackPressed , as you are going to close the Activity using a close button. In that case you need to call the finish() method explicitly on pressing the close button in your Activity .
  • If you intend to close all other Activity in the back stack, while starting a specific Activity , you don't need to call the finish() method either after starting each Activity that requires to go to that specific Activity . You just have to change the launchMode of that specific Activity in the manifest file to do so.

     <activity android:name=".ClearBackStackActivity" android:label="You want to clear the back stack of activities" android:launchMode="singleTask"> 

Update

So for clarification, I have a quiz activity that allows you to answer questions. When you submit the quiz you are taken to a results activity. Ideally, would I want to set the quiz activity itself to be cleared after the results are tallied and passed to the results activity? Would that achieve the desired goal and be memory efficient? I don't know if this makes sense what I'm asking here.

As far as I have understood, you want to clear the quiz activity after all answers are submitted and you have moved the user to the result activity. In this specific case, I can suggest you two different ways.

  • You might consider having a ResultActivity as your launcher. To start the quiz you might consider staring the QuizActivity using startActivityForResult and then after the answers in the QuizActivity are submitted, you are calculating the score and pass the score to the ResultActivity again using setResult from your QuizActivity and then call finish() method on your QuizActivity . You will be receiving the score passed from QuizActivity in the onActivityResult function in your ResultActivity .
  • The other way of doing this is having a QuizActivity as launcher which will have the questions and after submitting the answers, the score will be calculated and the ResultActivity will be opened with the score earned. I would like to suggest both of the activities to have launchMode=SingleTask . By clicking a button in the ResultActivity will start another QuizActivity .

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