[Q] Back Key behavior can be modified?

528 posts
Thanks Meter: 117
By leboreiro, Senior Member on 18th April 2014, 11:58 PM
Post Reply Subscribe to Thread Email Thread
So i have a galaxy s4. i had a idea, about pausing the process when leaving an app when hittin back button instead of destroying it.

when we open any app , bbm, facebook. games, twitter por example. and when we go out from it by pressing continously the back key, when we try to open the app again it will take more time than if we tap the home button.

basically home button tells android to pause the process and backbutton to close it which require more time when reloading the same app again.

i beleave this is not a remmaping thing because the back button will still going steps back in the apps, so, when there is no more steps to go back then it will pause it (not close it) ; home button will remain the same, pausing all apps to go home screen. and back button will pause only the one we are on (instead of closing it)

i repeat a lot the same thing because i want to be very clear. dont take it bad please.

Is this possible to make? may any of you give me some advices to do it?
19th April 2014, 05:28 AM |#2  
arpitkh96's Avatar
Senior Member
Flag panipat
Thanks Meter: 590
Donate to Me
You can change back button behavior by overriding this method
public void onBackPressed(){
Do here}

Sent from my GT-S5570 using XDA Premium 4 mobile app
19th April 2014, 07:05 AM |#3  
Masrepus's Avatar
Senior Member
Thanks Meter: 93
But only for your own app, this doesn't work globally

Phone : Nexus 4
Pure KitKat 4.4.2 stock, no root, no mods

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk
23rd April 2014, 11:45 AM |#4  
Jonny's Avatar
Forum Moderator
Flag Cheshire
Thanks Meter: 8,831
Donate to Me
Also overrides for the back keys won't work with fragments without some very hacky code, this is how I managed to do it:
public class NewsActivity extends SherlockFragmentActivity {
	private final static String TAG_FRAGMENT = "TAG_FRAGMENT";
	private static final int CONTENT_VIEW_ID = 666;

	protected void onCreate(Bundle savedInstanceState) {
		FrameLayout frame = new FrameLayout(this);
		setContentView(frame, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		if (savedInstanceState == null) {

	private void setInitialFragment() {
		final NewsFragment fragment = new NewsFragment();
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		fragmentTransaction.add(CONTENT_VIEW_ID, fragment, TAG_FRAGMENT).commit();

	public void onBackPressed() {
	    final NewsFragment fragment = (NewsFragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
	    if (fragment.onBackPressed()) {
} - only the bit needed for getting the back press code, the layout I'm using is the Navigation drawer, the code captures the back key press and opens the drawer on back key press if its closed (which is the opposite way of doing it from the default way but its how I need it for the app
	public boolean onBackPressed() {
		if (mDrawerLayout.isDrawerOpen(newsListView)) {
			return true;
		} else {
			return false;
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes