0

I am creating an application in which i want such permissions which donot allow my application to run in background.Even if back button is pressed or home button is pressed.

Like:-

android.permission.xxxxxxxx

i searched a lot but i didn't be able to find any result any help will be appreciable

  • you want to finish your app ? – Charaf Eddine Mechalikh Apr 21 '15 at 20:20
  • like in android we have a home button which when pressed once exit application and shows home screen and send application to run in background so this i dont want. – user3809212 Apr 21 '15 at 20:24
  • According to the [classic Android Lifecycle Diagram](http://developer.android.com/images/activity_lifecycle.png), in either `onPause()` or `onStop()` you could [terminate your app](http://stackoverflow.com/q/6330200/1270789). – Ken Y-N Apr 24 '15 at 04:36

3 Answers3

1

add this to your activity

 @Override
    public void onBackPressed() {
     finish()
    }

for home button it seems that you can't change its onPress function look here Home button click event handling android

try this instead

@Override
        protected void onStop() {
Taost.makeText(this,"finishing the activity",Toast.LENGTH_LONG).show();
        finish();
        }
Community
  • 1
  • 1
Charaf Eddine Mechalikh
  • 1,248
  • 2
  • 10
  • 20
0

What about handle button click in code. Like in code below for back button.

@Override
    public void onBackPressed()
    {
        if (end_butt == 1) this.finish();
        else
        {
            end_butt = 1;
            Toast.makeText(this, "Press back button once more to end application.", Toast.LENGTH_LONG).show();
        }
    }
Bluf
  • 250
  • 2
  • 14
  • Like in android we have a home button which when pressed once exit application and shows home screen and send application to run in background so this i dont want. – user3809212 Apr 21 '15 at 20:26
  • After pressing home button application will not run in background, if you do not have some background services. Application will be paused or stopped, it will nothing to do. I think override methods like OnStart, OnStop... Can handle your problem. Or you can identify navigation button and handle touch method. – Bluf Apr 22 '15 at 08:44
  • after pressing home button it shows home button again when i press home button and selecting application will start from the position where i stoped so how can we stop that – user3809212 Apr 24 '15 at 16:53
  • Try to use override method OnStart(), it is called every time when is your activity showing. Look on activity life cycle. You can combinate methods OnPause and OnStop with OnStart. – Bluf Apr 25 '15 at 17:15
-1

Assuming you call no activities yourself, you can do this

public void onStop(){
    finish()
}

THat will kill your activity whenever your activity is no longer fully visible. Of course it will also fire if you purposely launch another activity. But this is the closest you're going to get I think.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • finish() method not found cannot resolve finish method – user3809212 Apr 21 '15 at 20:46
  • Your activity has a finish. That's where it needs to go. IF you don't know what finish is and where onStop is I really suggest you read some android tutorials. – Gabe Sechan Apr 21 '15 at 20:48
  • i i have done exactly same and run the application then pressed home button once it displayed home screen after that i pressed my home button for a little long then it lists all the background app running in that that was there – user3809212 Apr 21 '15 at 20:54
  • That isn't a list of background apps. That's a list of recently used apps. It will relaunch you with your original intent via that menu, but that's a relaunch not an unbackgrounding. If you want to not be in the list of recently used apps that's a totally different question. – Gabe Sechan Apr 21 '15 at 21:23