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
}
Related
I am testing my android app that is in development.
It contains a single MainActivity
I noticed through testing that onDestroy was being called every time I push the back button.
I thought that this was weird. So I created a fresh empty activity app using android studio, and added no code. Just a simple hello world.
Even in this hello world app, onDestroy is being called everytime I press back.
I am running a Samsung S4 and I have not reason to believe that it is resource starved. What is going on here?
I tried setting android:launchMode to all available values in AndroidManifest.xml, and none of that worked....
Activity getting destroyed whenever you are pressing back button i.e android default behavior. This is how code flows.
override onBackPressed inside your Activity
/**
* called when user press back button on device
*/
#Override
public void onBackPressed() {
super.onBackPressed();
}
Go inside onBackPressed which lies inside FragmentActivity which shows that it will first pop all fragments from activity and then it will finish activity.
/**
* Take care of popping the fragment back stack or finishing the activity
* as appropriate.
*/
public void onBackPressed() {
if (!mFragments.getSupportFragmentManager().popBackStackImmediate()) {
supportFinishAfterTransition();
}
}
It is normal and expected behavior that OnDestroy() is called after the back button is pressed. This is a standard part of the Android Activity Lifecycle. You can read about the lifecycle here: https://developer.android.com/guide/components/activities/activity-lifecycle.html
I would not recommend overriding the back button behavior as jitesh has suggested unless you have good reason. Users will expect that your app is "closed" (destroyed) once the back button is pressed.
If you want OnDestroy() not being called everytime you tap back button:
#Override
public void onBackPressed() {
// super.onBackPressed(); // remove this line
}
I have a list view in the previous intent and when I tap on that it go to the next activity. Now when I press the back button or up button it comes back to the previous activity. But when I tap the list item again it give me run time Unexpectedly app closed error. Please help me.
It's hard to answer without code, but maybe it's because you don't finish your next activity when you press the back button
It's because of Activity life cycle...
When you go to the next activity then current activity is paused for you. when you press the back or up button then your previous activity resume.
That's why you have to define your code in onResume() method also where you got stuck. because when you come back onResume method is called
You can try override onBackPressed method in your activities :
#Override
public void onBackPressed() {
// put a breakpoint here and check your listview/activities states
//super.onBackPressed();
}
My app starts with the MainActivity, which is nothing but a simple menu with buttons for sub menus:
Start
Options
Help
etc...
Each sub menu option starts a new activity.
These new activities (submenus) will have a Back button on the top in the Title bar.
When I press these back button on the top, it goes back to the Main Menu activity as expected, but it calls onCreate function of the Main Activity.
This is not good, it equals to an app restart in my case.
If I press the physical back button and I implement the onBackPressed function,
it can resume to the Main Menu without calling the onCreate function,
which is my desired behaviour.
#Override
public void onBackPressed()
{
super.onBackPressed();
}
How can I force the Title bar Back button not to call the onCreate function of the caller activity?
Is there any other function I have to override to make it not happen?
Thank you
When you press the Up button the parent activity is called. Sort of like a startActivity call, which creates a new instance of the chosen activity.
To prevent that, you can specify that only a single instance of your Start activity can be created. Set this in your manifest:
android:launchMode="singleInstance"
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));
}
So my login Activity is the first screen you see. When you hit the back button, it exits the app, good. So I open up the app again. After logging in, I am now in my main Activity. How do I make it so when I hit the back button now, it exits the app rather than going back to the login Activity?
When you push the new activity, call finish() on the previous, otherwise it will remain on the stack, therefore appearing when you hit back and pop the current activity.
Hope that helps.
try this one
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}