i know that may duplicate some threads but i can#t figure out what i wrong. I have slider dreawer where are fragments and in fragments there are webview. Everything is working fine at least one thing, namely when i press back button it closes the app. I have tried some other possible solutions but anything is not working. I even don't get any errors. I even tried this easy solution but without any progress
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
My Main activity:
And this is one of my fragments:
By default back button shall close the app, if you are at the main/landing activity(there are other ways as well). If you want to override backbutton behavior, you should be overriding onBackPressed(), which you are doing right, but you should avoid calling super.onBackPressed() (since this gives you the default behavious, i.e. closing the activity OR finish(), with this you're closing the activity yourself. which is what you want to avoid.
Hope it helps.
#Override
public void onBackPressed() {
if (mSlidingDrawer.isOpened()) {
mSlidingDrawer.close()
} else {
Toast.makeText(MyTestApplication.getAppContext(), "Closing application", Toast.LENGTH_SHORT).show();
super.onBackPressed();
}
}
Related
I am developing application in which I have three fragment A,B,C, when I press on some button in A it navigate to b Thats fine ,and when I press device back button application closes instead of going to A,How can I prevent this problem .
// Your Main Activity
// Override OnBackPressed Event Which as below
#Override
public void onBackPressed() {
if (getSupportFragmentManager()
.getBackStackEntryCount() > 0) {
super.onBackPressed();
} else {
UIUtils.showAlertDialog(this, getString(R.string.app_name), "Are you sure want to Exit App?", false);
}
}
Just override the activity's onBackPressed() function according to your need.
You should override onBackPressed() method. There you can choose what action to do when this happens. If you still want to finish the activity in some cases, you can call finish() method.
Please add
addToBackStack(null);
to your FragmentTransaction object if you not added when replacing or
adding fragments.
it will autamatically maintain backstacks on Backpress.
Hope it will help you !
I am using navigation drawer in my MainActivity.java to switch fragments and I am also extending my MainActivity to the other activities in my app. Now the problem is that, when I press back button during I am on fragment, the app suddenly exits without any notification. And if I use OnBackPressed in my MainActivity it is bydefault implemented to the other activities extending main activity too and when I press back button to any of that acivity it asks me first for confirmation and then comes back to previous activity or frag. Need a solution to avoid this. I want to set onbackpessed or anything that shows dialogue or asks for confirmation to exit app on fragments only but dont know howto do.
Or any help regarding fragment back stacking to open a fixed fragment on back button pressed is welcomed if it tells how to change the title of hence opened fragment too.
call onBackPressed() in that activity in which you have put all fragment or main activity
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() == 0) {
//Display your dialog here
} else {
getFragmentManager().popBackStack();
}
}
Other solution
#Override
public void onBackPressed() {
if (slidingMenu.isMenuShowing()) {
slidingMenu.toggle(true);
} else {
super.onBackPressed();
//Display your dialog
}
}
This is works for me.
I'm using this template https://github.com/kanytu/android-material-drawer-template just to try out material design so I've implemented a few fragments some have webviews some have not.
My problem is when switching between the fragments I can see them being successfully added to the backstack
getFragmentManager().beginTransaction().replace(R.id.container, new FAQ()).addToBackStack("FAQ").commit();
But when I press the back button it just closes the app.
When I change it to use Activity instead of ActionBarActivity the navigation works fine but I lose some other functionality.
There is an override on the back button
#Override
public void onBackPressed() {
if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
super.onBackPressed();
}
but even if that's removed it still happens. I think the problem lies somewhere in the super.onBackPressed
Is there any reason ActionBarActivity would break the back button?
I recently read a post about this, sorry I can't find it anymore... But basically, it explained that the primary function of the back button is to finish the current Activity.
In fact, according to the onBackPressed() official documentation:
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
And it would appear that even though the back button used to pop the backstack before 5.0, Google would have changed this behaviour with the new ActionBarActivity.
For my part, I used some workarround that works for me but that might not work for everybody, depending on your navigation implementation.
But in case it could be helpful to somebody, here it is :
#Override
public void onBackPressed()
{
if (mDrawerLayout.isDrawerOpen()) {
mDrawerLayout.closeDrawer();
} else if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
This way, ActionBarActivity.onBackPressed() is only called when the backstack is empty, in which case it destroys the ActionBarActivity.
You should check "getFragmentManager" & "getSupportFragmentManager" is matched your activity & actionbaractivity or not.
Because, in Activity:
public void onBackPressed() {
if (!mFragments.popBackStackImmediate()) {
finish();
}
}
in FragmentActivity:
public void onBackPressed() {
if (!mFragments.popBackStackImmediate()) {
finish();
}
}
We can see the same code which already handled pop fragments backstatck.
In my situation, I used actionbaractivity(extends FragmentAcvitiy), but I also used "getFragmentManager" , so I got the same error as you. After I have replaced "getFragmentManager" to "getSupportFragmentManager", that's ok! You also can replace "actionbaractiviy" to "Activity" to fix this problem.
Must ensure "getFragmentManager" match "Activity", "getSupportFragmentManager" match "FragmentActivity(ActionbarActivity)".
If you want add actionbar On API level 11 or higher, You can see below:
https://developer.android.com/guide/topics/ui/actionbar.html#Adding
On API level 11 or higher
The action bar is included in all activities that use the Theme.Holo theme (or one of its descendants), which is the default theme when either the targetSdkVersion or minSdkVersion attribute is set to "11" or higher. If you don't want the action bar for an activity, set the activity theme to Theme.Holo.NoActionBar.
I would like handle back button in my application i have used fragment class in whole application .So when i would like to come back to base activity,it throw out application,Please give me any suggestion.
You just need to override the onBackPressed method on your activity.
#Override
public void onBackPressed() {
//Put your logic here :)
}
I dont understand what you want to do, but if you want to implement your own code in the back button this is how you do it.
#Override
public void onBackPressed() {
// your code
}
If you do nothing, then nothing will happen on a back press.
Edit: perhaps the above is not at all what you want, I've some difficulties understanding you. However, if you properly want to understand activities and how they are managed / created this is a must read. http://developer.android.com/guide/components/tasks-and-back-stack.html
My application should ask the user for a confirmation before putting the application in the background when the user presses the back button.
I tried to override dispatchKeyEvent. The problem is that I also have fragments that are pushed in the backStack.
I should not ask the confirmation when there is still a fragment in the back stack because in that case the application won't go to the background: it will pop up the fragment from the stack.
Is there a way to distinguish between the case when the application will go to the background and when another fragment will be popped up from the stack in dispatchKeyEvent?
If not is there another way to do it?
Thanks
You can override the onBackPressed method and get a list of current tasks from the activity manager and then decide weather to ask the user for conformation or just go back. This solutions is discussed here.
just override onBackPressed.
Also, see http://developer.android.com/guide/components/tasks-and-back-stack.html for better understanding of the notion of backstack in android
use this method of Activity
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
// Add here whatever uoy want
}
You can override
public void onBackPressed() {
super.onBackPressed();
}
and check for your condition like,
public void onBackPressed() {
if(foo == true)
showDialog();
else
super.onBackPressed();
}