How to Handle back button for Fragment in android? - android

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

Related

How to use setContentView(layout_id) second time in main_activity of android code?

My activity_main.xml has 2 buttons. I have implemented onClickListner(); for both of them .
For MainActivity, - > setContentView(R.layout.activity_main)
This activity_main has the 2 buttons.
Button1 - setContentView(R.layout.layout1);
Button2 - setContentView (R.layout.layout2);
Is this the proper way to use?? Because..
The program runs fine. The problem is that when I click Button2, the layout2 loads, I want to come back to main_activity now, So I press 'Back' button.
The entire app closes..!! I am taken to homescreen of the phone.
How to get around this?? I say ViewSwitcher. I do not have a button to come back.
Any other way?? Please excuse for basic question and bad English.
U have to override onBackPressed() function in your activity and again u have to setContentView to activity_main.xml and remove the super.onBackPressed from onBackPressed.
Something like this:
#Override
public void onBackPressed() {
//validation if you are in second layout
if(layout2){
//do things
showLayout1();
}else{
super.onBackPressed();
}
}
You should pretty much never call setContentView() more than once. What you're describing is standard backstack behavior. Either start a new Activity whatever layout you're transitioning to, or switch to using Fragments, and add a new Fragment to the backstack instead of calling setContentView().
Both approaches will give you native behavioral support for the back button.

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

Stop showing add onBackpressed

I am using startApp-SDK . on pressing back button from mainActivity. it shows an add by default. I want to stop showing that add on Backpressed. MainActivity extends from TabActivity and contains three tabs on pressing back from any of them show ads. I have override onBackpressed. in all of them. but still showing those ads.
#Override
public void onBackPressed() {
super.onBackPressed();
}
and also tried some other way around but problem don't solve ..
Thanks in advance..
I have find the solution to my question . May be it help someone else..
whlile declaring StartappAd
StartAppSDK.init(this, "105605040","205467351", false); // true for showing adds.
//NOTE: If you don't wish your application to display ads when pressing the Home button,Back Button simply pass false in the last parameter

Disable back button in Android doesn't work

I'm programming an Android app API 11 and I used it in a TabActivity:
#Override
public void onBackPressed() {
}
But the back button still works, why? Do I have todo something diferent in a TabActivity?
Ok, easy answer, I had to put it into the child activities of each tab.

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