Android navigation drawer exits on back pressed - android

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.

Related

How to close a fragment when back button is pressed?

I have an activity wherein if i click the "BUY" button, it will open a fragment about (150x150 pixels) to ask for the "quantity". If the user will press the back button, it will just simply close the fragment. Any Ideas about closing the fragment?
If you really want to use Fragment instead of dialog as suggested and want to remove it, keep a reference to it when creating it and then use getFragmentManager().beginTransaction().remove(fragment).commit() (you can also detach instead of remove if you only want to detach it from the activity but not destroy it completely).
You don't need to do it in a fragment it's too much for this simple use, i suggest you to use a PopupWindow:
When you click on Buy Button, you show the popup.
PopupWindow is enough customizable to make your layout as you want
here an example : https://android--code.blogspot.com/2016/01/android-popup-window-example.html
Use popbackstack on backpress of activty by overriding it.
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}

How to prevent application closed on pressing device back button?

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 !

how to maintain the fragment depend upon back button?

I've got a navigation drawer. In that navigation drawer I've got five fragments. When I select a fragment, it shows up. When I press the back button, it goes to the previous fragment. However, I need the back button to send you back to the first fragment. How do I do this?
Overwrite the onBackPressed() method in your Activity:
#Override
public void onBackPressed(){
FragmentTransactionn ft = getFragmentManager().beginTransaction();
ft.replace([your fragment container], yourfirstFragment, TAG_FRAGMENTFIRST);
ft.commit();
}
Apparently you already figured out how to show fragments. I'd suggest using the same code you use in your navigation drawer in the public void onBackPressed() to draw up the first fragment again. To make users able to exit the app, check if the first fragment is already visible. If so, call super.onBackPressed() or finish().

Detecting the visible fragment in android

I want to implement the onbackpressed() in android and my code is as follows
public void backpressed(){
NDListeningFragment fragment1=(NDListeningFragment)getSupportFragmentManager().findFragmentByTag(ConnectedDevicesFragment.TAG);
if(fragment1!=null && fragment1.isVisible())
{
super.onBackPressed();
}
else
{
fragment1=(NDListeningFragment)SimpleFragmentFactory.createFragment(ConnectedDevicesFragment.TAG);
getSupportFragmentManager().beginTransaction().replace(R.id.content,fragment1).commit();
fragment1.setUserVisibleHint(true);
}
}
The above code checks if the visible fragment is ConnectedDevicesFragment. If yes then super() is called and if not then I create ConnectedDevicesFragment and replace it in the framelayout.
But I am not able to implement in this way. When I press back button it reloads the Connected DevicesFragment again and again.
can you help with some workaround.
Cheers!
You creates fragment1 object every time in the onBackPressed function it means it will not null and it is on invisible state. You need to add NDListeningFragment in backstack when you open NDListeningFragment first and check Is the fragment available in back stack. If yes then call super.onBackpressed.

Webview back button is killing all activity

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();
}
}

Categories

Resources