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.
Related
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();
});
I am using a Full-Screen activity in my app in android studio, and everything works fine, but when the splash screen is over (after 3000 ms), the main activity or home starts, but if I use the back button from the android phone, the display goes back to the splash screen and it stays on this splash screen (you can not go back to the main activity again)...
What code can I use to avoid this? (only show once the splash screen when the app is launched and never go back to it even if I press the back button).
Thanks in advance..
You need to clear the back stack when you go from the splash screen activity by setting the FLAG_ACTIVITY_CLEAR_TOP & FLAG_ACTIVITY_NEW_TASK to the intent.
So, in the splash screen activity:
Intent intent = new Intent(this, HomeActivity.class); // or MainActivity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
I found the answer to this. If you want to prevent the MainActivity from going back to the splash screen, you can use the next method in the Main Activity:
#Override
public void onBackPressed() {
moveTaskToBack(false);
}
This method will also prevent the user from getting out of the app if they press the back button (so they stay on the app until they press the home button or the other button).
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
I am developing an application and its Home Screen Widget.
Now from my widget when i press on a button it would open up my application from where it was left.
Means if i press home button during my application running then my application will go in background mode.
Now i want that it should resume my opened application.
Whenever i press a button from my Widget. How Can i Do it??
Please help
Thanks a bunch in advance!
I've never made a widget before, but this is how I've made a notification launch back into my original activity when you pull down the bar and click it.
Intent originalActivity = new Intent(getApplicationContext(), Widget.class);
originalActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Once again, not sure how you would do this with a widget, but to relaunch it for a notification you convert that Intent into a PendingIntent to be called later when you want to launch back into it. I would assume this is a similar fashion for how you would it on a widget.