11

I have an activity which is purely transparent (user can't see when it gets open).I open this activity from background service whenever I want to open it. After doing some work, I need to close that activity (not finish()).I want to hide or minimize app. Actually, my app opens up for a second and do someWork after doing someWork, it continues to do remaining work (some uploading process) in background i.e. in onPause.I don't want user to know that something opens & closed immediately.

Currently, I am using below code-

public void minimizeApp() {
 Intent startMain = new Intent(Intent.ACTION_MAIN);
 startMain.addCategory(Intent.CATEGORY_HOME);
 startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(startMain);
}

Now, Problem is that If user is using some other app eg, playing game or using Whatsapp, When my app get opens over previous app(Game or Whatsapp) and minimizeApp() is called then it minimises my app along with other app opened in background. I want to minimize only my app.

karanatwal.github.io
  • 3,613
  • 3
  • 25
  • 57
  • Just curious, why do you need an activity to do some work? Is `Service` not good enough? – Sangharsh Feb 06 '17 at 13:21
  • @Sangharsh, services sometimes can't do all the works we need.Some tasks can be done only from `activity` class. And in my case, i did every task silently in transparent activity, so that user can't see what's happening.It's kind of spy app. – karanatwal.github.io Feb 06 '17 at 13:27
  • can you try solution provided in this link? http://stackoverflow.com/questions/10461095/moving-application-in-background-on-back-button-event – rohitanand Feb 06 '17 at 13:57

1 Answers1

34

You can try the following from the activity/context from where you want to minimise the app:

YourActivity.this.moveTaskToBack(true);
asdec90
  • 1,070
  • 2
  • 13
  • 28