Post Reply

[Q] Back Key behavior can be modified?

OP leboreiro

18th April 2014, 10:58 PM   |  #1  
leboreiro's Avatar
OP Senior Member
Kakadam City
Thanks Meter: 95
 
412 posts
Join Date:Joined: Jun 2013
Donate to Me
More
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, 04:28 AM   |  #2  
arpitkh96's Avatar
Senior Member
Flag panipat
Thanks Meter: 384
 
1,098 posts
Join Date:Joined: Feb 2012
More
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, 06:05 AM   |  #3  
Masrepus's Avatar
Senior Member
Thanks Meter: 74
 
667 posts
Join Date:Joined: Feb 2013
More
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
23rd April 2014, 10:45 AM   |  #4  
Jogny's Avatar
Forum Moderator
Flag Cheshire
Thanks Meter: 8,059
 
8,008 posts
Join Date:Joined: Jul 2011
Donate to Me
More
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
Code:
	public boolean onBackPressed() {
		if (mDrawerLayout.isDrawerOpen(newsListView)) {
			return true;
		} else {
			mDrawerLayout.openDrawer(newsListView);
			return false;
		}
	}
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Top Threads in Java for Android App Development by ThreadRank