I tried some way to exit from application but each method just minimizes the app. I'd like to close down the app not minimize it. Exit Button is in MainAcitivity.
For now i'm calling this function, it works but not as i need.
public void AppExit()
{
this.finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
I know this question is asked a lot but i can't find solution exactly what i need.
After your advice i use this function
public void AppExit()
{
System.exit(0);
finish();
}
If you want to exit why you are starting activity again.
use only this.finish(), remove other statements.
Take look at activity life cycle for more info
Few things. In your code you can put finish(); after startActivity, this may help (did for me).
How it should be done however is the way the guidelines have it already. The back button does close the app. If you press the back button and then restart your app, you will be on the very first page activity, not where you left off.
In regards to the back button, you can Override it to control what it does if you are not happy with it. You can put your code in there and when the users presses the back button it will execute the code. Users are more accustomed to pressing the back button rather than a button within a layout (which I presume is what you may be doing).
This might be helpful if you are sticking to what you have: How to clear the Android Stack of activities?
Try with this single line code :
System.exit(0);
You need to navigate to your main activity with Intent.FLAG_ACTIVITY_CLEAR_TOP, pass it some data that it will know that you are exiting the application. And just call finish from your main activity.
Another suggestion is Kamlesh Arya answer. How ever it is a bad practice as it will not destroy the activity stack, and next time the use lunch your application it will not start from the main activity but from the last active activity.
Related
I have an Activity A which is listening to some websocket message, when it receive certain message, it will navigate to Activity B by startActivity().
Things works fine when App is on foreground, but when it's on background, the startActivity() will cause the App bring to front automatically.
How can I "sliently" startActivity() in background without bringing
the app to foreground?
You can't. What you can do is set a flag, then in onResume call startActivity for the next activity if the flag is set.
Or use fragments instead of activities.
It is not directly possible to do so, However you can try something like minimizing the app as soon as the activity gets started.
In the onCreate method of your activity use the following statement:
YourActivity.this.moveTaskToBack(true);
Of course you will need to use some flags and extras to prevent it from being minimized in other cases too.
as #Gabe said, You can do this by setting a flag, same as we do open login or main activity by checking user object in SharedPreference.
if(SharedPreference Message count > 0){
startActivity(new Intent(this, YourChattingActivity.class));
}
Also you will need to set message count in SharedPreference.
Using below link you are able to achieve what you want:
Here is the solution to check activity status and handle according to that.
I have some floating bugs in my app, which unable to reproduce clearly. I suspect them from inproper work of my SaveInstanceState|restoreInstanceState mechanism, so I need to check case, when activity is being stopped when goes to background, and recreating after I press back button from spawned activity. Is there a way to force android stop and destroy activity which went to background? It should remain on activity stack, so I cannot just finish it.
Just enable the developer option "don't keep activities" (or whatever its called). This won't remove the activity from the stack, but will actually call onStop() and onDestroy() whenever the user leaves the activity and opens another. When the user presses BACK, Android will create a new instance of the activity, and call onCreate() and onRestoreInstanceState()` as expected.
Looks like it's a copy of this and this questions. Override onPause() method and call method finish() in it.
I go through few activities and when I want to go back to main window I use FLAG_ACTIVITY_CLEAR_TOP but if I press back button then I would go back to other activities that I went through.
Briefly speaking I want to go to main activity and after presing back button it wouldn't go to other activities.
EDITED
This clossing all activities is used to complete logoff process in another words it shouldn't user let to go back. I can't use BackPressed(), because I am using putExtra and getExtra. Maybe someone could write small sample. Because I don't understand flag system.
Use FLAG_ACTIVITY_NO_HISTORY on Activities you don't want saved on the stack. When the user presses back he/she will go to the last activity that didn't have that flag set.
You can override onBackPressed in your activity and start your desired one with the flags:
FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP
Im needing to know how to destroy previous class, when I open a new class so I dont have a ton of open classes to have to hit the 'back' button through to get back to 1st main class.
I know the layout code within the onCreate is typically this:
setContentView(R.layout.page);
i was thinking like super.destroy();
So when I use an intent to launch another class, I want the inital class to not leave behind open window/ContentView
By class, you mean another activity?
You can call finish(); after you have started the intent;
Intent intent = new Intent(activity1.this, activity2.class);
startActivity(intent);
finish();
Android also takes care of these things himself. If memory is needed, the Dalvik virtual machine will close low priority activities that are running.
onDestroy is called automatically when the Activity is shut down. To remove your previos layout you can just call the method finish() or System.exit(0);
While searching this question I have seen this. One time Android setup screen?
It asks my question, but I can never get it to work. What I want to happen is when I first startup the app, it gives me the setup screen. Then when I press save, I want it to quit the app. When I click the app again, I want it to preform a task, rather than show up with a screen. So thats really 2 questions, how to make it show a setup screen one time, and then how to make it do an action(by clicking on the app) without a screen showing up at all.
Right now, I use SharedPreferences editor to input my settings.
Pretty simple, in the onCreate of your activity:
savedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
if(savedPreferences.getBoolean(PREF_SHOW_ABOUT_ON_APP_START, true)){
Intent intent = new Intent(this, SetupActivity.class);
startActivity(intent);
savedPreferences.edit().putBoolean(PREF_SHOW_ABOUT_ON_APP_START, false).commit(); // YOu could do this line within the SetupActivity to ensure they have actually done what you wanted
finish();
} else {
// Go somewere else
}
You don't have to activity switch, but you get the picture
For the first time only set up screen, you simply have to use SharedPreferences. Check if a value, alreadyLaunched exists in the shared preferences. If not, show the set up screen. If yes show your app.
For the second part of your question, it would be considered very bad practice to have an icon app that does nothing at all when you click it. How would the user know that it worked or did what it was supposed to do ?
For the "I want a task to execute but I don't want the user to see it happening", I believe you should look at services. When your user closes the app clicking the save button on the set up screen, start a service ( http://developer.android.com/reference/android/app/Service.html )
When the user clicks again on your app, simply show a message and make the app call the service via an intent.
Probably you are forgetting to call
myEditor.commit()
after you are done with setting your preferences.