How to prevent application closed on pressing device back button? - android

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 !

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

Android navigation drawer exits on back pressed

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.

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

disable back press only when the application would otherwise go to the background in Android

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

Categories

Resources