Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to minimise/hide app programmatically?

Tags:

android

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.

like image 285
karanatwal.github.io Avatar asked Feb 06 '17 13:02

karanatwal.github.io


1 Answers

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

YourActivity.this.moveTaskToBack(true);
like image 139
asdec90 Avatar answered Oct 06 '22 13:10

asdec90