The "finish" function in an Android app closes the application. As an Android app developer, you must create a button for your users so they can close the app when they're finished with it. Create the button in your Java XML file and use the Java source code to trigger the code that closes the app.
XML
- The "main.xml" file contains the buttons and properties for your Android app. You must add the button in the XML file before you can use it in your app to close the program. The following code adds a button you can use to close the app:<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/self_destruct"
android:onClick="selfDestruct" />
Java Code
- After you create the XML code for the button, you can set up the code that handles the "finish" function when the user clicks it. The following code closes the application when the button is clicked:Intent intent = new Intent(Intent.ACTION_MAIN);
finish();
Testing
- You use the Eclipse software to create Android apps. Eclipse includes a debugger and emulator. These functions let you run the code in an Android development environment so you can test for any coding errors. You use the debugger to test the new exit button. Click "Run" and tap the button to check for any coding issues when the app closes.
Considerations
- If you want to offer a close button, prompt the user to save a file if a file is opened that must be saved before closing. The app programmer must include this functionality in the code or the Java application will close without saving the user's changes.