Pressing back button reduces counter - android

I have made a simple app on Android which increases the counter value by one and goes to next activity. At this point, if i press the back button, instead of just leaving the app or going to homescreen of the app, it goes to previous screen and reduces the counter by one.
How do i prevent this from happening and keep the counter value same.
Thanks

use like that
#Override
public void onBackPressed() {
super.onBackPressed();
counter--;
// fireintent here or finish() call
}

You could intercept the back click and handle it differently:
#Override
public void onBackPressed() {
// do something else.
}

When starting your new intent make sure you include NOHISTORY to prevent the activities piling up on the stack. You will need to save the value into your applications shared prefrences or another file to ensure that the value will be kept when your application exits.

Related

Make App Launch Activity not be visible in application manage after back is pressed

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();
}
}

Do certain action every time the app is closing

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.
}
}

Destroy an activity when back button is pressed?

I have an app that allows user to select a txt file from a list and then goes off to the internet to get the contents of that file. All works well except when the user accidentally or deliberately presses the hardware back button to go and see the list again.
Now, when the user clicks a new item from the list (a new file that is), instead of loading a the new file, the app continues off from where it was suspended.I do not want that to happen. I know this is related to the life cycle of the activity.
How do I make sure that it loads the new file rather than continuing from where it left off ?
I suppose you're loading the file in onCreate(). You should do that in onResume() instead.
Do not force Activities to close (e.g. use finish()). First, this does not guarantee the Activity will be closed, and second, this is better left to Android to decide.
Another Method to kill an activity is by calling following method.
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
You can override the onBackPressed method in the activity and call finish() to get the desired outcome.
You just need to finish your activity in onBackPressed() method by calling activity.finish();
To destroy activity on back press, use this code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Destroys activity.
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!

Android screen management

This is a very basic question, I have a few screens, now when you go from one to another you can then press back and cycle back through all the windows.
I'd rather that when you pressed back it took you to a specific window for instance:
Menu Screen
---->User clicks Info
Info Screen
---->User clicks Ride Info
Ride Info
---->User clicks back
Info Screen
Now is this to do with the hierarchical parent, will this define where it goes back to?
The second part of my question is if I don't have any resources to release or information to store for an on-resume what should I do when the user pauses my app? At the moment if you go back to the menu screen and re-select the app it will start a new instance rather than resuming. Do I just simply implement:
#Override
public void onPause() {
super.onPause(); // Always call the superclass method first
}
and
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
}
Apologies if this is a bit basic!
You might want to look in to setting FLAGS for your intent while opening new activity Android Dev
Something like this -
Intent a = new Intent(this,A.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);
There is no basic questions here :)
Easiest way to do this is to override the onBackPress() function.
Sample :
#Override
public void onBackPressed() {
//Do what you want here
}
For saving variables when users leave the app, you need to override onSaveInstanceState(Bundle bundle)
#Override
protected void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(outState);
bundle.putInt("sample", 1);
}
For your two parts:
1) It's almost always best to let Android handle the back button, the order of which is determined by the back stack: see here for an explanation. If you want hierarchical navigation, I would recommend looking into the up button - see this developer page for a good explanation of how to use the different navigation tools.
Additionally, if you don't want to have an activity appear in your back stack, you can set the attribute android:noHistory="true" in your manifest, which will mean that the user can't return to it using the back button.
2) If the user has left your app, it's automatically paused, you don't need to implement onPause or onResume for this to happen. However, it's also up for collection to be terminated by the OS. If this happens, then it will be restarted when the user opens it from the launcher again. Any previously running instances of the app should automatically be opened.

Categories

Resources