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();
});
Related
I have an activity in my application that does a process and when finished, launches an activity with a normal intent:
Intent intent2 = new Intent();
intent2.setClass(anActivity.this, JustAnotherActivity.class);
startActivity(intent2);
finish();
The problem is that when you are doing the process, if I press the home button and take it to the background, when it finishes and calls the new activity, even if it is in the home of the phone, this new activity jumps to the foreground.
I am looking for the activity to be launched, but if the user is outside the application, it does not come to the foreground.
I guess the use of a FLAG is necessary, but the ones I have tried have not worked.
sorry for my bad english
Thanks.
what if you take care of ActivityLifecycle (OnPause, OnStop).
You can add a flag when the activity goes "OnPause/OnStop" depending what you want. And them perform the intent just if the activity is not "Pause/Stop".
Activity Lifecycle
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);
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
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.
i have a button to close my app with this code:
finish();
the problem is that this button doesn't exit of my app... it simply closes the current intent ant returns to the previous intent (window) of my app....
how i can do a real exit/close button?
i tryed with this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but it doesn't works, because when i turn back into my app, the app comes in the last opened window, and not in the first window of the app, how i can do that? i need that when i re-open my app it starts on the first window of my app
If you really want your app to die. You could initiate each intent with startActivityForResult(). then before each finish() set the result to send back. in each parent activity you can override onActivityResult() to test whether the result received means the application needs to end. if so you can call another set result and finish(). repeat this in all activities and you will find that your application terminates entirely.
Incidentally I'm writing this from memory. function names may not be exact.
Hope that helps.
p.s. re-read your requirements. you can always stop the finish loop at your first activity.
I would do it this way:
I would define my initial activity (i.e. MainMenu) with a Launch Mode of singleTop
I would then invoke my MainMenu from the activity that is going to close the application.
startActivity(new Intent(this, MainMenu.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("closeProgram", true);
Then override the onNewIntent in the MainMenu activity; check for the extra boolean of "closeProgram", if the value is true, then do a finish();
Haven't tried it but I think it should work.
I recommend you read this: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html
Chances are, you don't want an exit button. Perhaps a logout button, but that's it.
finish() closes activity (this is what you call intent, but it's not correct), not application. Application can not be finished at all (only forcefully killed like task killers do). You should design your activity stack in such a way that it will suit your needs. May be you should look at stack rearrangement examples in ApiDemos.
you can try this: System.exit(0);