0

I have one problem regarding my app,when i click back button my application is getting finish.i need to prevent it.How could i make it .

@Override
public void onBackPressed() {
   Log.d("CDA", "onBackPressed Called");
   Intent setIntent = new Intent(Intent.ACTION_MAIN);

   startActivity(setIntent);
} 

I have tried something like this.But its not working .Actually what i need is when i will click back button ,the application should not finish it should run in background.can anybody help me out.@Thanks

  • does it finish with crash/error? – waqaslam May 13 '13 at 07:29
  • what does mean by 'should run in background'.. is there any special codes what what you want to do? – Pankaj Kumar May 13 '13 at 07:31
  • what the mean `run in background`, ? either you can disable back button or don't finish activity before you call new activity in stack – Chintan Khetiya May 13 '13 at 07:31
  • @Waqas no it is not showing error/crash, but i need that on click back button the application should not finish ,it should always run in background –  May 13 '13 at 07:32
  • @chintankhetiya how do you disable backbutton – Raghunandan May 13 '13 at 07:32
  • Use a service if you want your app to keep running in the background. – Vikram Gupta May 13 '13 at 07:32
  • @PankajKumar run in background means the activity should not close it should keep working –  May 13 '13 at 07:33
  • @vickey how could i make it, –  May 13 '13 at 07:33
  • An activity is a single, focused thing that the user can do. You need to create a service to do the background work. – Tarun May 13 '13 at 07:34
  • Use Services for this... can't be implemented via Activity – Pankaj Kumar May 13 '13 at 07:35
  • take a look at these: http://developer.android.com/reference/android/app/Service.html and http://www.vogella.com/articles/AndroidServices/article.html – Vikram Gupta May 13 '13 at 07:36
  • http://stackoverflow.com/questions/4779954/disable-back-button-in-android @Raghunandan is it okay ? i am using this and disable as per needs – Chintan Khetiya May 13 '13 at 07:37
  • @vickey is there any FLAG that will keep the application on running in background –  May 13 '13 at 07:39
  • No there isn't. There is an Activty method moveTaskToBack(boolean nonRoot). Have a look at that. Although I strongly urge you to use a service. – Vikram Gupta May 13 '13 at 07:41
  • @vickey how could i used that service in activity –  May 13 '13 at 07:44
  • You create a service just like an activity and bind it to your main activity. Once started, Services are designed to run in the background. Click on the following links to understand services: developer.android.com/reference/android/app/Service.html and vogella.com/articles/AndroidServices/article.html. – Vikram Gupta May 13 '13 at 07:46

2 Answers2

2

Try doing it like this:

@Override
public void onBackPressed() {
    Log.d("CDA", "onBackPressed Called");
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);

    startActivity(intent);
}

By doing this, your activity will not be destroyed (i.e. onDestroy will not be raised). But also, there's no guarantee that Android will preserve your activity for long.

In case, you are running a process that you want to keep on running even in background, then I would suggest you to go for Service or IntentService.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
2

What do you mean by "it should run in background"? Why would you want that? If the user wants to keep the app opened, he can use the home button and the app won't be closed, if he presses the back button he wants to close the app. If want to have something that is still running even after the user closes the application you should take a look at Service http://developer.android.com/reference/android/app/Service.html, this will continue running even after the user closes the app

bogdan
  • 782
  • 3
  • 7