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 !
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
}
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 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));
}
I have an android app. The used cases are as below
From activity A can navigate to activity B via startActivity() and activity A is still on the 'STACK' (A is not finished).
Activity B is a subclass of google MapActivity
On Press of the BACK button , navigation should be from B to A.
The above use case works fine until I upgraded to Android 4.1.1(works fine from 2.3.3 to 4.0.x).
On my current 4.1.1(Samsung S3), the "BACK' button usually not working. When I press the BACK button, the button light up but no action is performed.
I even override the OnPressBack() method and placed a Toast message as a flag. But looks like the OnPressBack() is not triggered usually. Sometimes it works but most of the time it does not work(go to previous activity A).
And I also found the BACK button issue only happened with the subclass of 'MapActivity'.
Any clue will help. Thanks...
If you're using the new navigation components, you need to make sure the activity that contains your navController overrides the onSupportNavigateUp() function:
override fun onSupportNavigateUp() = findNavController(R.id.nav_host_fragment).navigateUp()
How to complete the application when pressed buttons Home and Back? Provided that in my memory holds many pictures.
When pressed on the Back button - application restarts... When pressed on the Home button - quit application, but when it restart - does not start Splashstsreen.
Hard to see without code but it sounds like your activity is resuming rather then starting from scratch (as it should behave). It sounds like it's behaving correctly in that case. If you insist in wanting your application to truly quit after the back button is clicked perhaps you can override onBackPressed() then have your Activity call its finish() method.
I don't think it's good programming practice to fiddle and interfering with the activities lifecycle. It's the OS responsibility to manage the lifecycle, including pause and finish activities.
Instead you should use other methods to handle your problem with not showing splash screen, these methods are onResume and maybe also onStart(). Also you should get familiar with the activity lifecycle(link submitted by #ss1271).
Press Home will let the activity to enter onPause(). however, if you insist to quit the application when press HOME, which is obviously not a good practise, you can do like this:
Override onPause() and onBackPressed()
add finish(); to these methods.
I am sure by adding finish(); to onBackPressed() will quit the app when Back button pressed, but I haven't tried on Home.
Please refer to Activity Lifecycle for more info.