How to minimise/hide app programmatically? - 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.

You can try the following from the activity/context from where you want to minimise the app:
YourActivity.this.moveTaskToBack(true);

Related

Android - Very slow finishing of an activity

I have a "Close" button which closes the application.
I have tried 2 methods but they both are very slow.
Finishing the activity:
activity.finish()
Sending Home Page intent (from here)
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
How can I close the activity or send it to the background faster?
Why clicking on the "home" button is much faster than sending ACTION_MAIN intent?
Pressing the HOME Button will call onPause() on the current Activity. Thus it's not closing the app in total but pause it.
Ergo: It is of course much faster even in starting because it keeps the memory.
Now you know how to pause the app instead of closing if you want to have a fast approach.
If you want to close the whole app I would suggest to use finishAndRemoveTask();
Finishes all activities in this task and removes it from the recent
tasks list.
Note: BACK Button will call onDestroy() if you want to have another way. Try out what fits best for your usage.
CODE EXAMPLE
Close app like HOME Button programmatically without a transparent View using a Button:
Button close = findViewById(R.id.myCloseButton);
close.setOnClickListener(view -> {
finishAffinity();
});

start an activity without show it

I was trying to start activity from a service without showing it to the user, keep it work in background, I was searching a lot about that, and I found two ways to do that
by starting the activity then start the home main screen like this :
// this is for lunch the app
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.some");
// this is for going back
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// lets do it !
startActivity(LaunchIntent);
startActivity(startMain);
by putting a going back function in the activity itself, like this :
context.getActivity().moveTaskToBack(true);
BUT, in these two ways I have problems
in the first one, if the user was on another app, this operation will close his app and get him to home , more than that, some times the my activity not started but just be in the back without working i.e. if there was a song it isn't played
in second one, when the my activity started a black screen will appear for a second before it back to the home or previous user app
So , simply , this is what I want :
I want a behaviour equal to : the user open my app then he press back button, but without show that the app started unless he see the background apps
how to do that ?
For that, you can use an android Service.
See docs - http://developer.android.com/guide/components/services.html

Android app starts fresh after intent action

I am launching skype with the below code from a button click.
Uri skypeUri = Uri.parse(uri.toString());
Intent myIntent = new Intent("android.intent.action.CALL_PRIVILEGED", skypeUri);
myIntent.setComponent(new ComponentName("com.skype.raider", "com.skype.raider.Main"));
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity(myIntent);
It gets launched as a seperate activity and my app goes in background. When call is done, and i click on my app from background running apps, it starts from first screen however the call was placed from third screen. Any idea where I am wrong in implementation.
I want my app to go to background as soon as call button is clicked. When i am done with skype call and starts it again from background running apps it should not start from first screen. It should start from that 3rd screen only where i left it.
Thanks

minimize application and starts from where user left

i am making an application and i want to minimize the application when user click the minimize button and i am using the below code.below code minimize the Application but when i re open the application it always starts with the first activity.And i want the the application should resume from where the user has minimized the application.. i.e. user minimizes application when he is at 3rd activity and when he re open it the application should start with the 3rd activity not the 1st.
Intent main = new Intent(Intent.ACTION_MAIN);
main.addCategory(Intent.CATEGORY_HOME);
main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(main);
Instead of your code try using
moveTaskToBack(true);
Read more about this function.

How to stop activity when my application icon is clicked again

When i click training icon it show the button named Home.
When i click home button it shows the home screen as below.
My application is currently running right, I want to close the application and have to show the home screen when i click the app icon(training).How could i do this..?
Actually i have a button in my layout and i'm showing the home screen with the following code when the button is pressed.
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
It shows the home screen and my service in background has started.
I want to stop all the action from my app to be stopped when i click launcher icon.
My question is how can i stop the activity when my application icon is clicked again.
Sorry for poor english..!
If you application consists only of an Activity then it is already stopped when the user re-clicks the launcher icon (since he had to exit the app).
We need more info if that's not your scenario.
Use it in your activity. When you exit from your app it will stop the action as well as destroy the app. And it will launch new activity when you click your app icon again.
#Override
protected void onDestroy() {
android.os.Process.killProcess(android.os.Process.myPid());
}
Maybe you can check if your service is running, or more dirty with a static int counter. Let the counter increment in your onCreate(). Now you can check for the second start of your apk
Put finish(); on the method onResume().
Of course, if you need to do something else, just put your code before the finish();.
It should do.

Categories

Resources