how to stay app open when the user press back button - android

I want to create an app with this ability:when the user press back button.don't close app & stay in running list in settings->application manager->running
& stay in running list when user press home button too.
I like to make an app that there isn't in task manager->active application but there is in settings->application manager->running such as Viber,Tango,Gmail

Add onBackPressed() into your Activity:
#Override
public void onBackPressed() {
moveTaskToBack(false);
}

you can achieve it as follow:
#Override
public void onBackPressed() {
moveTaskToBack(false);
}
to minimize your app use moveTaskToBack(true);
NOTE: don't write super.onBackPressed();

Override onbackpress and do the code for application to go background

Why do you need to keep your app running? If you need to process data while the user is not in your app, you need a Service:
http://www.vogella.com/tutorials/AndroidServices/article.html
Android simple service not starting

Related

how we handle onBackPressed in an android Service

I develop an application of cracking screen i run an android service to showing crack screen on home screen now i want to repair when user pressed back button on five times while our service is running on top and show crack screen how can i handle back button within a service please help me for this how we handle back button in a services like this app https://play.google.com/store/apps/details?id=ultimate.apps.crackscreen2
public void onBackPressed() {
super.onBackPressed();
//Do ur operation here
}

How to close activities which are appeared only once?

I am creating an app which consists of main, register, option activities. Main and register activities should be started only once. I got it. Problem is they are again appearing when I just going back to my mobile home page without clicking exit. Even if I do not click the exit and goes back to home, they should not appear again when I entered!! Is this possible? I am not able to get the solution for this. Any one please help me.. Thankyou
you can override the method onStop() and put finish() there.
So, when the activity will be not longer visible, it will finish. Or you can put finish() after startActivity() call method.
you can do this by.
Button button15 =(Button)findViewById(R.id.button5);
button15.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
}
}
);

How to clear activity stack trace on back press event?

I have created an application that has multiple pages and navigation from one to another represents a crucial flow. I don't want the user to be able to press the back button and escape the activity without first warning him and then finally deleting all stack trace such that when the activity is launched again it starts afresh.
As of yet I have been using something similar to the function below :
#Override
public void onBackPressed()
{
this.finish();
Intent int1= new Intent(this, Home.class);
int1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(int1);
super.onBackPressed();
}
But sometimes when after quitting the application when I launch it again it restarts from some random page or the one from where I quit the application (basically not the home screen from where it is expected to start)
I cannot think of a cleaner way to quit the application other than clearing all the previous activity flags as described in the code.
Any help on the above is appreciated!
EDIT :
Anytime during the flow of my activity if the user presses the back button, I want the control to be thrown back to the main page (clearing all the previous activity stack traces). Such that in case someone re-lanches the application it will re start normally from the main page.
You don't need any of this custom code in onBackPressed(). All you need to do is add this to all of your <activity> definitions in the manifest (except the root activity):
android:noHistory="true"
This ensures that none of your activities (expect the root activity) is recorded in the back stack. When the user clicks the BACK key, it will just return to the root activity.
Another benefit of this is that if the user leaves your app (by clicking HOME or by pulling down the notification bar and clicking on a notification, when he returns to your app it will also just return to your root activity.
Anytime during the flow of my activity if the user presses the back
button, I want the control to be thrown back to the main page
(clearing all the previous activity stack traces).
This can be done just by finishing all the activities as they move forward, except the MainActivity.
Such that in case someone re-lanches the application it will re start
normally from the main page.
Is it like if user is in Activity_5 and uses Home Button and relaunches the app again, MainActicity must appear?
IF so, you can call finish() in onPause() of every Activity except MainActivity
EDIT:
Might not be the perfect solution, but this is what I did to achieve exactly the same(logout in my application):
OnBackPressed() in any activity updates a boolean shared preference say backPressed to true and in onResume() of all the Activities, except MainActivity check its value and finish if true.
#Override
protected void onResume() {
super.onResume();
SharedPreferences mSP = getSharedPreferences(
"your_preferences", 0);
if (mSP .getBoolean("backPressed", false)) {
finish();
}
}
Back Button is used to go back to the previous activity. So i would not override the back button to clear activity stack. I suggest you use a Action Bar for this purpose. Navigate to Home Screen of the application using the application icon.
http://developer.android.com/guide/topics/ui/actionbar.html
Also check this link and comments below the answer by warrenfaith
android - onBackPressed() not working for me
#Override
public void onBackPressed()
{
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
you can use that code, it's work for me!

when touch back button on android how to quit the app by cocos2d-x

A game created by cocos2dx. In active scene, when I touch the back button on android, how can i quit it!
can give same example?
Version cocos2d-2.0-x-2.0.4 and cocos2d-x-2.1.4
in our Layer.h:
...
void keyBackClicked();
...
in our Layer.ccp:
Layer::init(){
...
this->setKeypadEnabled(true);
...
}
void Layer::keyBackClicked() {
CCDirector::sharedDirector()->end();
}
Now you are in a position if you click back then you are navigated to the previous screen where you started off your game application right? Then here is the solution: after navigating to the new intent, the previous screen gets in an inactive state in android activity lifecycle you can find that the previous screen goes to a invisible state. Now we can use onPause() method to close the hidden activity. In the same class add this code and your app should be closed when you press back button.
protected void onPause() {
super.onPause();
finish();
}
When you click any button and go to a new intent the application goes to the invisible state and the onPause() method is triggered automatically and it closes the same intent in background.
Override backKeyClicked() method in your layer.
Don't forget to add this->isKeypadEnabled(true) in init method of layer.
In you backKeyClicked method, you can switch it to previous scene or whatever you wanna do.

Android - How to exit an app when user press the home button?

I want to know how to exit an app when user press the Home Button.
As far as i know that Home Button moves the running app in background and puts Launcher process in front.
I know i can use finish() but i don't know where i should call it because i have no idea which function is going to get a call when user will press the Home Key.
Thanks.
Depending on what you want to do, overriding onUserLeaveHint might be the best bet:
http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
This will let your app know that your app is being exited because the user chose to switch apps (Like by hitting the Home button or selecting a notification). This hint function will not be called if an incoming phone call (or similar) is causing your app to go to the background, as the user didn't initiate that action.
In my case,
I have many activities in application
so if I use 'onUserLeaveHint', 'onStop' or ...
It just catch activity transition.
So I put below code every activities.
And finally catch user's home button action.
first, you have singleton object
public class ActivityContext {
public static Context context = null;
}
and every activities,
#Override
protected void onStop(){ //onStop runs after New one's onResume
super.onStop();
if(ActivityContext.context==context) { //user home button out
AppStartEnd.sendAppEndData(context);
}
}
#Override
protected void onResume(){
super.onResume();
ActivityContext.context = YourActivity.this;
}
I hope this will help you

Categories

Resources