Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

test: Reply
 
LinkBack Thread Tools
Old December 27th, 2012, 08:47 AM   #1 (permalink)
Member
Thread Author (OP)
 
Join Date: Nov 2010
Location: London
Posts: 210
 
Device(s): HTC Desire HD, Samsung Galaxy SII, Samsung Galaxy Nexus, ASUS Nexus 7
Carrier: Not Provided

Thanks: 16
Thanked 8 Times in 7 Posts
Post 5 Fragments - get back button to return to certain Fragment? It closes app at the moment.

Hey all.

I've set up a horizontal scrolling system to move between 5 different Fragments, at the moment pressing the back button on the phone/tablet will exit the app. This happens on all Fragments.

Can anyone help in getting the back button to move to the first fragment. So Fragment 2, 3 , 4 and 5 will all move to Fragment 1 when the back button is pressed.

I've been looking into Fragment transactions but I can't get them to work, I guess I just don't understand it that well yet. Maybe it's not the correct way.

I have the horizontal scrolling thing set up in the usual way. Each Fragment has it's own Java file, there's a Fragment pager adapter. based within a FragmentActivity.

So can anyone give me some advice on where to go with this?
Appreciate any help!

Advertisements
__________________
Twitter.

My Android apps: Link to play store.
RED_ is offline  
Reply With Quote
sponsored links
Old December 27th, 2012, 09:26 AM   #2 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

Have you tried overriding the back button?
__________________
My blog, Inverted Keystrokes, is looking for aspiring developers (not necessarily with Android) to post articles. If you have any development experience and are interested in participating, please PM me. =)
jonbonazza is offline  
Reply With Quote
Old December 27th, 2012, 09:55 AM   #3 (permalink)
Member
Thread Author (OP)
 
Join Date: Nov 2010
Location: London
Posts: 210
 
Device(s): HTC Desire HD, Samsung Galaxy SII, Samsung Galaxy Nexus, ASUS Nexus 7
Carrier: Not Provided

Thanks: 16
Thanked 8 Times in 7 Posts
Default

Quote:
Originally Posted by jonbonazza View Post
Have you tried overriding the back button?
I haven't. At the moment I have an AlertDialog that asks the user if they want to exit the app. I want the user to be able to get back to Fragment 1 but then when they hit the back button on Fragment 1 the AlertDialog comes up. I don't overriding the back button can achieve this.
RED_ is offline  
Reply With Quote
Old December 28th, 2012, 10:56 AM   #4 (permalink)
Member
Thread Author (OP)
 
Join Date: Nov 2010
Location: London
Posts: 210
 
Device(s): HTC Desire HD, Samsung Galaxy SII, Samsung Galaxy Nexus, ASUS Nexus 7
Carrier: Not Provided

Thanks: 16
Thanked 8 Times in 7 Posts
Default

Quote:
Originally Posted by jonbonazza View Post
Have you tried overriding the back button?
I'm currently looking into this, is there any chance you could explain how I would go about overriding the back button? Thanks.
RED_ is offline  
Reply With Quote
Old December 28th, 2012, 11:16 AM   #5 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

Add the following to your activity:
[high]
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
}
return super.onKeyDown(keyCode, event);
}

private void onBackPressed() {
//Add fragment handling code here
}
[/high]
jonbonazza is offline  
Reply With Quote
Old December 28th, 2012, 11:35 AM   #6 (permalink)
Member
Thread Author (OP)
 
Join Date: Nov 2010
Location: London
Posts: 210
 
Device(s): HTC Desire HD, Samsung Galaxy SII, Samsung Galaxy Nexus, ASUS Nexus 7
Carrier: Not Provided

Thanks: 16
Thanked 8 Times in 7 Posts
Default

I'm getting the following error with "OnBackPressed()":

"Cannot reduce the visibility of the inherited method from FragmentActivity"

I'm putting it in my FragmentActivity because that's where all the Fragments are handled with the ViewPager. Each Fragment has it's own java file so it's not a subclass of an Activity.


EDIT: If anyone else finds this I achieved what I wanted by overriding the back button with an if statement. Calling it to set the ViewPager's currentItem to the desired page if it wasn't or call a DialogFragment class that I created which simply contained an AlertDialog.
RED_ is offline  
Last edited by RED_; December 30th, 2012 at 09:23 AM.
Reply With Quote
Reply
Tags
android, application, back button, building, fragmentpageradapter, fragments, horizontal, java, scrolling, viewpager


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 11:58 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.