I want to create a very simple Android application called "Loyalty Card" to display the bar-code image for my food store on a telephone screen. The bar-code reader at the food store check-out can see this image and recognize me. I've tested this concept and it seems to work well.
However, since I only visit the food store weekly there's no reason to leave the application running (or even idle) in the background after the bar-code is scanned. It seems sensible to me to end, terminate, annihilate the application once I've checked out. And I think a reasonable user could see value in having a "close" button to do that.
By using onClick in the application XML and finish() within the Java method I've managed to get the close button to return from where it came. (By that I mean if the application was launched from the home screen by a shortcut then my close button returns to the home screen. If the application was launched from the app drawer the close button return to the app drawer.) In either case the application list still contains the running/idle application. I really, really want the application to end, stop, self-destruct, so to speak.
In reading about this topic I recognize that many people think this attempt to close an application using a button is not good practice. But for this use particular case the user likely won't want to pull up the bar-code again until next week, when he/she shops for food.
In my humble opinion I'd like the user to be able to click on the close button (a single step) and have the application itself completely stop itself.
The alternative is to expect the user to tap the back-button (or home), then the application-list button, perhaps scroll to numerous running other running applications, then a right-swipe to manually halt the application (four steps).
I don't know about you but I usually have a long list of running applications and seeing an extra application unused laying around is kind of a nuisance. Especially since in this use-case the user isn't likely to want to see that bar-code again for a week.
With that said, I'll admit I'm new to Java programming, much less programming for the Android platform. I'd like a suggestion for how to rewrite the Java code shown below so the closeIt method completely and absolutely destroys itself and (I'm guessing here) the main process.
Here's the Java code I've coded, based on searching this topic:
package com.example.loyaltycard;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/* Close the application without leaving it in the activity list */
public void closeIt (View view) {
finish();
return;
}
}
The XML to go with this Java code is shown here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.loyaltycard.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/barcodeimage" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal= "true"
android:onClick="closeIt"
android:text="Close" />
</RelativeLayout>
Finally, thank you in advance for allowing me to write a long and detailed question. And I'm hoping we can avoid the discussion of whether this is a good practice. Rather I'd like to find a solution. And I promise not just to cut/paste your ideas, but research why what you suggest should work. So, you know, I truly learn from you.
EDIT: I'm using Android Studio as a development environment, which may complicate this, I think.