15

I want to "minimize" the application, leaving it in background doing exactly the same that when the home button is pressed when the user clicks a button (but don't finish it) How can I do that?

Sufian
  • 6,405
  • 16
  • 66
  • 120
Addev
  • 31,819
  • 51
  • 183
  • 302
  • Possible duplicate of http://stackoverflow.com/questions/4647231/android-minimize-application – Jack Nov 23 '11 at 01:32
  • 1
    possible duplicate of [Sending Activity to background with out finishing](http://stackoverflow.com/questions/2041891/sending-activity-to-background-with-out-finishing) – Ted Hopp Nov 23 '11 at 01:36

2 Answers2

23

You can use the moveTaskToBack(boolean) method of Activity.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
-2

Short answer:

you can't.

Explanation:

The android Activity Life-cycle does not give you that level of control. If the Android OS needs the memory, or the phone interrupts your activity, it may be killed.

Then again, it may not. Android will pause you activity, and application, and if possible it will not destroy any Activitys.

The Fix:

You need to listen for the onPause() and onResume() events in your Activity. And you have to serialize and deserialize your data in each case, saving the state of the Activity when it is paused and resumed.

There's some good explanation of how this works on the Android developer site, here.

And if you simply want a Button in your app to act as the home button, you create an Intent for that with ACTION_MAIN and category CATEGORY_HOME.

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
Sufian
  • 6,405
  • 16
  • 66
  • 120
Plastic Sturgeon
  • 12,527
  • 4
  • 33
  • 47