I'd like to do certain, simple action every time that my app is closing - I just want to decrease int variable. How to do that?
As #Ozair has rightly asked that it depends what do you define as closing of app.
You can do either or both of the following depending on the need:
If you want to detect closing of app by use of BACK button, then from within your last activity, you can detect the pressing of BACK button by overriding onBackPressed function. There you can decrement your value.
If you are also considering the situation when you app goes into the background by pressing of HOME button, then in your activities you would have to detect the HOME button pressed. There have been many solutions which no more work for detecting HOME button but this answer on How can I detect user pressing HOME key in my activity? question seems to work for me.
So, there you can detect the HOME button and decrement the value which you can save in SharedPreferece.
There can be other cases where you are calling finish() and closing your last activity. It is not clear from your question if you are considering that case as well.
Hope this gives you some opportunity to think about it.
The question is what you mean "close"?
If you close all your Activities, the App-process might still be running. If you mean that the "close" is just closing all of your Activities. You might define a "count" for all opening Activities, you can store it in DB or SharePerference. I think you can do follow(dummy codes):
In your project, you should define BasicActivity:
public class BasicActivity extends Activity {
onCreate() {
mPreference.incActivityCount();//++
super.onCreate();
}
onDestory() {
mPreference.decActivityCount();//--
if( mPreference.getActivity() == 0 ) {
//All being opened Activities have been closed.
onAppHasNoUIs();
}
super.onDestory();
}
onAppHasNoUIs() {
//All being opened Activities have been closed.
}
}
Related
i have two simple questions (hope so).
First i want to close my app, when the user hits the homebutton in my HomeFragment, because there is a counter (Days,Hours) and when I click the home Button and come back later there is still the old value.
Then i have to close the app via backbutton and start again. So i want my app to get killed, when I hit the home button.
Then i have a button to delete the last value, on which the counter is based. When I hit the button i want to run my homeFragment again. Should i just create a funnction which calls, the homefragment.onCreate again?
When I hit the button i want to run my homeFragment again. Should i just create a funnction which calls, the homefragment.onCreate again?
No, these functions are meant to be called by the system.
You are probably looking for the onStop() method (also called by the system)
protected void onStop()
{
super.onStop();
// clear counter e.g.
myCounter.setText("");
}
Other than this, it's kind of hard to tell what you want. Feel free to add more (if not all) of your code.
Basicly, i have 3 activities in my app. call them A,B and Login, A and B shows the user very sensitive data. a legit flow has the form of launch->Login->A->B->A->B...->A->quit what i really want is that if for some reason the app got to the background ( by pressing back or home or whatever ) while it was still on A, or on B, then no metter what way the app relaunches ( or traced in some way including by the long home press menu ) it would not show A or B content. he can see either the login page content, or nothing at all.
noHistory is almost what i was looking for, but isnt a good solution since i want to allow intuative navigation from A to B and back to A. so how is it done?
Check out the "clearTaskOnLaunch" Activity attribute.
When the value is "true", every time users start the task again, they
are brought to its root activity regardless of what they were last
doing in the task and regardless of whether they used the Back or Home
button to leave it.
try the following pseudo:
public A extends Activity
{
boolean just_launched = true;
void onPause()
{
just_launched = false;
}
void onResume()
{
if (!just_launched)
finish();
}
}
I want a notification to pop up when an app opens. But once User dismisses, I don't want it to come back again, even if they go back to the same activity. But when the app exits, and they come back later, I want same dialog notification to pop up (prompting user to login).
So basically...
boolean b = true;
if (b == true) {
// show dialog
b = false;
}
I simply want var b to save state but clear on exit.
I'm not sure there's a point to using sharedpreference if you don't want the value to persist across your app being closed, as that's kind of the point of them.
Perhaps just using a boolean in your Application class would be better? It will be persistent until the app is completely shutdown.
android has no concept of "app exit", only the android lifecycle, so you'll have to be more specific about the behavior you want. for example, do you mean whenever the user causes any activity in your app to be paused and no activity in your app to be resumed?
if you only have one activity, then you can just keep a state variable in the activity setting it to false when paused and true when resumed.
if you have >1 activity, keeping track of when none of your activities are in the foreground any longer is painful. for example, if you set foreground=false whenever an activity pauses, and foreground=true whenever it's resumed, and then check a time tolerance. e.g., if the time between pause and resume is greater than some threshold, assume you are coming back from the background.
You don't want to use SharedPrefs unless they logout out of app with a button or some other listener, you can't be sure to change it. You can put it in onDestroy() to reset the variable or onPause() even better if you want to make sure it gets reset
I would like to create a button in my game for the user. The button will quit the application.
Please can someone tell me if there is a way to do this.
Thanks.
Edit:
I have an activity which uses another activity with a class that extends Android.app.Application using set and get methods.
Simply using the back button switches the activities until it goes to the beginning.
I go in between these classes 20 times.
Thats why I needed a back button. But I guess there isn't so I will have to do it the long way and set everything back to the first state on quit. Thanks
There is not a way to make quit button. And there is good reason for that because the Android experience is having the back button do the closing. So you just to make the back button exit back to the home page. To do that you need make sure that your current activity is the only one oh the history stack. Then you can create a button that just calls finish(). Hope the detail explanation helps.
You probably want to mange the activity stack better.
If you look at Activity and Task Design Guidelines
it might help.
Setting the flags when you start each activity is probably the key, code such as
Intent i = new Intent(this, Whatever.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
will limit the number of instances of 'whatever' to one only. ( A different flag might be more appropriate for you depending on how you want your app to run, read up about them all)
Try this:
public void quit(View view) {
if(Build.VERSION.SDK_INT>=16 && Build.VERSION.SDK_INT<21){
finishAffinity();
} else if(Build.VERSION.SDK_INT>=21){
finishAndRemoveTask();
}
}
If I read your full question, you are looking for a Reset button not exactly a quit button. I had a similar issue... the next and previous takes only one step back at a time. I wanted to go back to the very beginning. The way I acheived this is to have a class to manage the pseudocursor.. basically an int that represented which resource to pick (I used a singleton). In the main activity's Menu (android.view.Menu), I added a reset/go to beginning option. This will simply reset the pseudocursor. In my activity class's onResume(), I had the code to get the resource from the singleton. So no extra coding was required there.
Instead of having this option under Menu, you can always have a button in UI which does the same thing.
I've heard that pressing the back button will essentially cause the current Activity to finish(). Is this always the case? Seems like it would be with the way it pops the Activity off the stack.
The one situation I'm not so sure about is when the root Activity in a Task has back pressed. I'm currently experiencing a very weird effect, described as follows:
On loading my application, the first Activity is for initialization, and once it finishes, it calls my main Activity (a TabActivity). This first init activity has android:noHistory="true" set in the Manifest so pressing Back from my main Activity won't go back to that. It goes to the Launcher. When I click on my App in the Launcher a second time, the initialization activity loads again, and loads the main Activity when done. Almost immediately after, it loads a second instance of my main Activity. But ONLY after the Application has already been run once, and was exited by pressing BACK from the main Activity. It does it every subsequent time until I force quit the app or load a new version from the IDE.
Based on this, I am suspecting some kind of Activity instance is lying around and being reused, since it only happens on the second+ time I run the application (and exit with BACK -- using HOME just returns to the last state of the app, no big deal). Anyone have any thoughts??
I've heard that pressing the back button will essentially cause the current Activity to finish(). Is this always the case?
No it is not. The most activities have this behaviour but not all. For example you could create a Dialog and set it setCancelable(false) and it won't close if you click BACK button.
Furthermore you could customize activity behaviour on BACK button pressed by overriding onBackPressed
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
About your application behaviour..Did you verify if the activity launcher is finished after it loads your main activity? I mean if the onDestroy() method is called. Maybe after it runs the main activity it remains there and when you click back you just go back to the old Launcher...
hope this helps..
Read through the Activity and Task design guidelines on the Android developer site; they explain how the Home and Back buttons work. Obviously, if you override the default behavior (as mentioned by hara above), the back button will not finish the activity.
On your specific issue, check your logcat. You should be able to see there whether it is bringing an old process back to life or starting up a new one. If that is unclear, insert a couple of log statements into onCreate, onPause, onDestroyed, etc., so that you can see exactly what is happening with your process.
You can control BACK-BUTTON by writing the following code.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK ) {
//preventing default implementation previous to
//android.os.Build.VERSION_CODES.ECLAIR
return false;
}
return super.onKeyDown(keyCode, event);
}
Are you running your activities with any special flags, such as singleInstance or singleTop? Those could be causing the oddities you're seeing. The easiest way to track down what's causing your problem is to absolutely fill it with debugging messages. For example:
In your initialisation activity, add a log in the beginning of onCreate to get the name of the activity such as this.toString(). More on why you want this line later.
When it launches the main tabbed activity, get the name of the launching activity and a message saying it's launched the tabbed one.
Override the onPause(), onStop() and onDestroy() callbacks and add debugging lines with this.toString() and also a message telling you which callback it is.
What this will do is tell you whether you've got multiple instances of the initialisation activity lying around. To this by comparing the name of the activities calling your main activity with the ones that were just created and the ones that went through to onDestroy.
If you don't know how to debug, use Log.d(LOG_TAG, "Your message here");. And then define a constant LOG_TAG String somewhere. After that, show the LogCat perspective in Eclispe by going to Window, show perspective (or view, don't remember exactly), other, Android, LogCat. The purpose of having a LOG_TAG constant is that you can set up LogCat to filter to that String and only show you those messages. It will make it easier to see them among the mass of system log messages.
The short answer to the original question is 'no'. This is largely because, unfortunately, not every developer follows the guidelines referenced by previous answers.
Yet the guidleines themselves mention exceptions, when the Back key should not call finish(). the most prominent exception is the Web browser, which has its own "back stack" for each window, so it must have its own custom handling of the Back key.
If there are no fragments on the back stack and a developer has not overridden onBackPressed, the activity will finish when the back button is pressed.
Here is the source code for Android 4.4.2 Activity.onBackPressed():
public void onBackPressed() {
if (!mFragments.popBackStackImmediate()) {
finish();
}
}
just override onbackpressed().. on back press this method will get execute remove super and do what u want to do.