Quit application button android - android

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.

Related

Creating a back button to a previous activity (CONCEPT ONLY)

Before I say anything else, I am not asking how to make this work for my program, I want to figure it out myself. I have already seen one billion snippets of codes and answers. But I think I am not understanding the concept behind it and would like to verify if my understanding is correct.
Creating a back button and making it work:
1) Find and edit the xml for the action bar so show a back button.
2) Use the buttons id to create OnClickListener.
3) In OnClick, use intent to previous activity.
Is it not this simple? What are the proper steps conceptually for make this work?
That might work, but it wouldn't be the norm.
Depending on how you launch an Activity, simply calling finish() on an Activity will take you back to the previous one.
easier to
1) use AppCompatActivity
2) enable homeAsUp (back button)
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true)
Android activities are stored in the activity stack.
So simply by calling the finish() method if you started the activity with startActivity()
this.finish()
or if you opened the new activity from another activity with startActivityForResult()
call the finishActivity() method from your code on the click of the button
this.finishActivity()
And it'll take you back to the previous activity.
Well..... I don't know this answer would be helpful but, you said
3) In OnClick, use intent to previous activity.
However, how about thinking this situation?
If there is fragment and you press 'back button' in there, you should go before fragment not before activity. :)
You said you want to figure it out yourself so, I won't say more(about codes) as you wanted. Good Luck!

android restore state of edittext after returning to the activity

I am beginner to android developing and I am dealing with a probably simply solvable problem.
I have a Main activity which is a menu and the user can click on of the items on menu. Then a new Activity is started using startActivity(new Intent("com.example.test.FIRSTITEM")); this activity starts a layout with few edittexts. I write something into them. Then to go back to the MainActivity (where the menu is) one have to press back button.
And now I want the behaviour that if I return to the Activity with edittexts, I want them to be in the same state - meaning the content wouldn't be cleared.
I've searched a lot and tried all options of android:launchMode="" (singleTop, singleInstance, singleTask).
I've also tried to use flags when starting new intent from MainActivity:
startActivity(new Intent("com.example.test.FIRSTITEM").setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP))
I've tried using onSavedInstanceState(Bundle bundle) but this one is never called when pressing back button (probably because the activity isn't really finished)
I am sure the answer is simple but I can't seem to find it. I must be missing some logic of how activities work.
Thank you for your help in advance.
I think this this could help you out.You can use
onsaveinstancestate() and onrestoreinstancestate().

Android up navigation for an Activity with multiple parents

I have a problem for implementing up navigation on an app with this navigation tree:
The standard implementation of the back button is fine.
The problem start when trying to implement the Up button.
What I expect:
when the user is on Detail 5 Activity and press the up button the app goes to List 3 Activity
when the user is on Detail 7 Activity and press the up button the app goes back to Home Activity
So in different terms, I'd like to have this behaviour on the back stack:
The Android documentation (Implementing Ancestral Navigation) advice to use the following code to handle up navigation:
Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();
But because the parent activity of the Detail Activity differs on the different navigation path I don't know which one it really is. So I can't call it in the Intent.
Is there a way to know the real parent activity in the Android back stack?
If not, is there a way to implement a correct up navigation in this app?
I will stick with my comment on Paul's answer:
The idea is to have a Stack of the last Parent Activities traversed. Example:
public static Stack<Class<?>> parents = new Stack<Class<?>>();
Now in all your parent activities (the activities that are considered parents -e.g. in your case: List and Home), you add this to their onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parents.push(getClass());
//or better yet parents.push(getIntent()); as #jpardogo pointed
//of course change the other codes to make use of the Intent saved.
//... rest of your code
}
When you want to return to the Parent activity, you can use the following (according to your code):
Intent parentActivityIntent = new Intent(this, parents.pop());
parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();
I hope am right (:
That's a tricky question and in my opinion really shows the difficulties in coping with the UX decisions of Android for the "up button". Therefore, there's not a clear-cut answer to your problem.
I have two possible solutions for you.
1. Mimicking the back button behavior.
You could consider adding an extra to the intent for launching Detail from one of its various parents. This extra would inform those activities which activity they would need to launch when android.R.id.home is pressed.
This would effectively mean that your app "goes back" to its common ancestor, instead of simply relaunching Home.
Another way of implementing this may be simply executing onBackPressed() instead of launching Home with Intent.FLAG_ACTIVITY_CLEAR_TOP, but bear in mind that the associated animation would be different than a normal "up" action.
2. Skip intermediate activites and go home.
Some apps treat the "up button" as a "home button". You might want to consider having it simply always relaunch Home with Intent.FLAG_ACTIVITY_CLEAR_TOP.
this is an old post for sure, but as I was studying the SharedPreferences, I think it could be a possibility to stack this information within a sharedPreferences data, and to modify its value each time before going down the 2 parents.
Then by reading it, you should be able to directly know your parent, and this without having to build a whole class for that.

Exit application programmatically

I have been searching for an answer but I couldn't find a proper one. The question is that I have a dialog themed activity on top of a normal activity. I would like to force the users to either read and click "OK" to the themed activity which will then transfer them to another dialog themed activity for some further questions or cannot enter the application. So, I would like to exit the application, on back press, and not just finish the themed activity that will reveal the content of my app. How is that possible?
If you start your dialog activity with startActivityForResult() you can send back the result RESULT_CANCELED from the dialog, and upon receiving this (in your main activity) you call finish().
finish() will do the perfect job for you ;)
but make Dialog not cancelable..
Edit (after problem description clarification):
As others said StartActivityForResult could work, with additional trick.
Because of the way you design your App (DialogActivity1->DialogActivity2), it might help to add following line in AndroidManifest file, for all your special dialog-look activities:
android:noHistory="true" or to set flag for intent Intent.FLAG_ACTIVITY_NO_HISTORY before u start DialogActivityN.
Both lines (from manifest or code) will make this actitivies not to stay on android stack, so when your MainActivity get result back, it will be result from last DialogActivity and than depending on result recieved you can either finish() or continue with execution of MainActivity..
when u start activities like this there is no need to call finish() to destroy them, u just start new activity and they will be gone from stack. Of course, in your case, last DialogActivity u will start with StartActivityForResult() and as I explained in previous paragraph MainActivity will do something based on received results.
However making user goes through these dialogs several times at the beginning application, is not something I would consider good practice and it can make your user just give up and go for some less annoying app. (don't get this wrong, it's just my advice to rethink about concept)
Hope you will solve it ;) Cheers
If you know about ActivityforResult then way is easier for you, First you need to start the dialog activity with method startActivityforResult... and then when dialog activity get close by back button you have to close it by Set result. In OnactivityResult method of start activity have to detect the same and close the same if setResult is not as according. Hope you got the point.

android, starting and exiting activities

I have not really understood the handling of activities and the stack.
I have 3 activities, A - a splashcreen, B- a menu and C another Activity. I start the splash and exits it after a while when the menu is started.
In code I handle them all like this:
startActivity(new Intent(this, ContactInfoMenu.class));
finish();
Now, if I start the app and goes A-B-C, when I hit "Back" in C screen I jump back to B-the menu. Another "Back" exits the application, just like I want.
BUT .. if I go A-B-C-B-C - the "Back" button in C screen exits the whole app instead of getting me back to the B screen?
Why is that? It does like that in all my "subscreens", I can only enter them once, if I enter them a second time the "Back" button exits the app. And I have not tried to catch the "Back" action anywhere? Shouldn't I always call "finish()" when I start a new activity?
Regards
Finish is good for leaving the current activity and going back to the previous one. Otherwise, try to avoid calling finish() if you can help it.
There are a set of flags that you can pass when you start an activity that do a better job of determining how that activity behaves on the stack. These include:
FLAG_ACTIVITY_NO_HISTORY - your activity will not remain on the stack after another activity covers it.
FLAG_ACTIVITY_CLEAR_TOP - a good way to pop off a bunch of activities when you need to "go back" to a certain activity.
Many of these flags can be set in the manifest. Reading up on them will give you a better idea about "The Android Way".
Basically, You don't need to call finish() every time you go to another activity. If system is low on memory it will close your activity instance by itself.
finish() is more often used when yor are inserting some information in one page and then moving on to some other page. In that case, you might need to fininsh your first activity.
But in case where you need to shuffle between views, you must not use a finish() function, because it will cause the whole application to finish.
Try using back button of your own in your views to shift between activities, where you can move to any other activity of your application or even to the Main Screen.

Categories

Resources