I have a short question:
I'm new in the worl of android and I started now to programm a little app.
Now I have finished it and debugged it on my Samsung Galaxy S3. The application has 4 layouts. With a button you go to the next layout. So, I will, that if when I press the back softkey on my device, that it goes back to the last layout (like from layout 4 to layout 3).
When I tested it on my device, it always closes the app if I press the back softkey.
What can I do, that by pressing the app will change to the previous layout and closes if I press the back softkey if the main_actity is showed?
Thanks a lot for every answer.
With best regards
You can respond to the back press in the Activity's onBackPressed() callback. Please be careful that whatever you do makes sense from the user's perspective. Going back one "page" in your view hierarchy and closing the app after all that seems fine.
What you call a "new layout" should be a new activity/Fragment. Nothing else
Period
Do not try to change this behaviour.
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
By your sayings, you want the back button to navigate between layouts. However the back button navigates through activities.
If you need the back button to do something else than navigating through activies, you have to include it in onBackPressed() method. For instance, if you want to move from layout2 to layout1 you an do this:
#Override
public void onBackPressed() {
//do something to hide layout2 and show layout1.
//You can use View.visibility or anything!
}
This is one of the plenty solutions.
You should enter these codes in current activity to goto wantedActivity when press back on your device :
onBackPressed()
{
startActivity(new(Intent(currentActivity.this , wantedActivity.class));
}
Related
I have an activity with two fragments, each fragment has a back button to the previous activity/fragment. Both the back buttons on the fragments work properly. However when i run the app on my android phone and i use the built-in back button to navigate to the previous activity, it displays a blank activity and then when i press the built-in back button again only then does it navigate to the previous activity. The problem is clearly the back burton than built in.Is there a way to solve this???
There is a method in the Activity called onBackPressed() which is called when the device back button is pressed. If you want to control what happens on back press just override it. To remove default onBackPressed action you need to remove the call to super.onBackPressed() and then you control what happens when back button is pressed.
#Override
public void onBackPressed() {
//super.onBackPressed();
// do something here
// or perhaps nothing at all
}
I need to remove some screens from the navigation stack depending on what button is pressed.
I have a Home screen and next to it there are 3 more screens where the user enters some data(let's call them S1,S2, S3).
S1 is opened from home, S2 from S1, S3 from S2.
Each of these 3 screens have a back and a next button.
If the current screen is S2 and I press the back button I would like to remove it from the navigation stack, otherwhise when I go back to S1 and I press the back button it will go to S2, which is wrong, it should go to home.
I found out that calling Finish() would do the work, but it is not working if I press the next button instead of back. Neither the NoHistory = true is working in this case.
Is there something that can manage this or do you have any idea to solve it?
When your screen is S2, when press the back button you could calling Finish() to remove it from the stack, when press the next button you could use StartActivity(typeof(S3)) to open S3.
You have 2 solutions there :
1) You can override the OnBackPressed method on your activity. This will erase the classic behavior of the back button. So, for example in S2, you can override your OnBackPressed in order to navigate explicitly to S1. But be careful with that, because in this case you should also override the OnBackPressed method of S1 in order to avoid going back to S2 when the back button is pressed.
You can override OnBackPressed on your activity this way :
public override void OnBackPressed()
{
//navigate to the screen you want to
}
2) You can also set, in your activity, NoHistory = true. In your question, you said that the NoHistory = false statement does not work for you, which is perfectly normal since you have to put it true if you don't want the activity in your history stack.
Hope it helped !
I want to disable back button from closing the app.
How can i disable back button?
You can override the onBackPressed() method and determine what happens in that method. Like this:
#Override
public void onBackPressed() {
// do nothing because you don't want them to leave when it's pressed
}
Just add that method to your activity class and you're good to go.
However, this is bad app design. What you would most likely want to do is make a dialog pop up that asks them if they are sure they want to leave. You would add the dialog code inside that method so that when the back button is pressed, the dialog pops up.
Generally, that's not a good idea. Users hate to feel "trapped" in your app.
Many users are able to start apps "on top of" other apps. When they hit "back" they may expect your app to stop, and the app they were in previously to appear. This is different from "home" where they expect all apps to go to the background.
Users familiar and comfortable with this functionality will not like it if you change "back" - although you may give them options like "press back again to exit" as some apps do. It depends on your particular situation.
So if you are in need of it, here is a good reference:
Android - How To Override the "Back" button so it doesn't Finish() my Activity?
I have an app that consists of two activities: the "main" activity (M) and the "settings" activity (S). S can only be launched from M. The purpose of S is to modify settings which affect M. S has a "done" button that finishes the activity and goes back to M (through M's onActivityResult method, with an Intent that contains the new settings for M to apply).
My problem is: if I go back from S to M using the hardware "back" button (instead of S's "done" button) the system brings M to the top without any knowledge of the modified settings. Can this behaviour be modified? Ideally I would like to alter the behaviour of the hardware "back" button when S is on top, so that it cause S to finish (that way M would get the new settings).
If that's not possible, and more generally: what would you do you to have the settings applied on a "back" button pressing?
You can simply override onBackPressed()
#Override
public void onBackPressed()
{
// check if settings have been changed
super.onBackPressed();
}
Since this is a "closing action" do the super call after you have done your other work.
Following up on comments left on blackbelt's answer (now deleted comments) you may want to consider, if you haven't already, asking the user if they are sure they want to exit without saving in case they went into settings and decided not to change anything. What if they press the back button because they decided not to save the changes? You may already have something in place for this like a cancel button.
you have to override the onBackPressed from Activity and manage the same logic from the done button
You can also introduce a new java class to your package with static fields holding your settings.
Write to them as user changes settings & read from them as soon as in Activity's OnResume() method or later as needed.
You can achieve what you want by overriding onbackpressed method
#Override
public void onBackPressed() {
Intent intent = new Intent();
//get your settings from your views
intent.putExtra("setting1","on");
intent.putExtra("setting2","off");
setResult(RESULT_OK);
finish();
}
The answers above will do what you want, however:
Have you looked into using the built in android SharedPreferences? That way changes to the settings (made in S) will be stored to the device and then you can tell activity M to look at the settings and update appropriately in the onResume method. Plus the settings will be saved forever and it doesn't matter what happens to S.
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.