[Q] Back Key behavior can be modified?

Search This thread

leboreiro

Senior Member
Jun 13, 2013
844
170
Raccoon City
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?
 

arpitkh96

Senior Member
Feb 17, 2012
1,156
645
27
panipat
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
 

Masrepus

Senior Member
Feb 12, 2013
767
99
But only for your own app, this doesn't work globally

---------------------------------
Phone : Nexus 4
OS:
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
 

Jonny

Retired Forum Moderator
Jul 22, 2011
9,293
9,616
Also overrides for the back keys won't work with fragments without some very hacky code, this is how I managed to do it:

NewsActivity.java
Code:
public class NewsActivity extends SherlockFragmentActivity {
	private final static String TAG_FRAGMENT = "TAG_FRAGMENT";
	private static final int CONTENT_VIEW_ID = 666;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		FrameLayout frame = new FrameLayout(this);
		frame.setId(CONTENT_VIEW_ID);
		setContentView(frame, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		if (savedInstanceState == null) {
			setInitialFragment();
		}
	}

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

	@Override
	public void onBackPressed() {
	    final NewsFragment fragment = (NewsFragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
	    if (fragment.onBackPressed()) {
	        super.onBackPressed();
	    }
	}
}

NewsFragment.java - 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 :p
Code:
	public boolean onBackPressed() {
		if (mDrawerLayout.isDrawerOpen(newsListView)) {
			return true;
		} else {
			mDrawerLayout.openDrawer(newsListView);
			return false;
		}
	}