Override Home and Back Button Is Case a Boolean Is True

Override home and back button is case a boolean is true


I was wondering if I can override the action of the back and home button is some cases.

Yes you can do override Home button.

I have developed an application which disable hard button, you can have a look.
I have taken a toggle button which locks all hard button to work except Power button

public class DisableHardButton extends Activity {
/** Called when the activity is first created. */
TextView mTextView;
ToggleButton mToggleButton;
boolean isLock=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView=(TextView) findViewById(R.id.tvInfo);
mToggleButton=(ToggleButton) findViewById(R.id.btnLock);


mToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
isLock=isChecked;
onAttachedToWindow();
}
});
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {

if ( (event.getKeyCode() == KeyEvent.KEYCODE_HOME) && isLock) {
mTextView.setText("KEYCODE_HOME");
return true;
}
else
return super.dispatchKeyEvent(event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub

if( (keyCode==KeyEvent.KEYCODE_BACK) && isLock)
{
mTextView.setText("KEYCODE_BACK");
return true;
}
else
return super.onKeyDown(keyCode, event);
}
@Override
public void onAttachedToWindow()
{
System.out.println("Onactivity attached :"+isLock);
if(isLock)
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
else
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);
super.onAttachedToWindow();
}
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tvInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<ToggleButton
android:id="@+id/btnLock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="UnLocked"
android:textOn="Locked" />

</LinearLayout>

Override back button to act like home button

Most of the time you need to create a Service to perform something in the background, and your visible Activity simply controls this Service. (I'm sure the Music player works in the same way, so the example in the docs seems a bit misleading.) If that's the case, then your Activity can finish as usual and the Service will still be running.

A simpler approach is to capture the Back button press and call moveTaskToBack(true) as follows:

// 2.0 and above
@Override
public void onBackPressed() {
moveTaskToBack(true);
}

// Before 2.0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}

I think the preferred option should be for an Activity to finish normally and be able to recreate itself e.g. reading the current state from a Service if needed. But moveTaskToBack can be used as a quick alternative on occasion.

NOTE: as pointed out by Dave below Android 2.0 introduced a new onBackPressed method, and these recommendations on how to handle the Back button.

Android back button not working after onKeyUp() is overridden

Because You're always returning false from it. It should actually be conditional & in other cases, you should call super.onKeyUp(keyCode, event);

Example:

 @Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (condition == true){
//do something
return false;
}
return super.onKeyUp(keyCode,event);
}

OnOptionsItemSelected Method not called on click of back button android.R.id.home

Finally solved.

Now, if you want to listen to clicks on any type of NavigationIcon in your custom toolbar (be it a Hamburger or up-caret or some fancy icon) then use setToolbarNavigationClickListener(). No need to use onOptionsItemSelected Method.
Thanks Protino for suggestion

// Add the backstack listener
getSupportFragmentManager().addOnBackStackChangedListener(this);

// Handle clicks on the up arrow since this isnt handled by the
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getSupportFragmentManager();

// Pop the backstack all the way back to the initial fragment. Customize if needed
//fm.popBackStack(fm.getBackStackEntryAt(0).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);

if(fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
toggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(true);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}

}
});


/**
* Called everytime we add or remove something from the backstack
*/
@Override
public void onBackStackChanged() {

if(getSupportFragmentManager().getBackStackEntryCount() > 0) {
Log.d("Called >0","false");
toggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
} else {
Log.d("Called <0","true");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(true);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
}

/**
* If you need to move backwards inside the app using the back button, and want to override the
* the default behaviour which could take you outside the app before you've popped the entire stack
*/
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStackImmediate();
} else {
super.onBackPressed();
}
}

Change a boolean value on home button click in Android

Look at onUserLeaveHint() :

Called as part of the activity lifecycle when an activity is about to
go into the background as the result of user choice. For example, when
the user presses the Home key, onUserLeaveHint() will be called, but
when an incoming phone call causes the in-call Activity to be
automatically brought to the foreground, onUserLeaveHint() will not be
called on the activity being interrupted. In cases when it is invoked,
this method is called right before the activity's onPause() callback.

make Boolean variable true inside onUserLeaveHint() as:

 @Override
public void onUserLeaveHint() {
super.onUserLeaveHint();
//make Boolean true here because this method first
//called when user press home key
}

how to override action bar back button in android?

I think you want to override the click operation of home button. You can override this functionality like this in your activity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}

Overriding the Home button - how do I get rid of the choice?

You can't permanently override the Home button without the user confirming it.

One argument for why this is the case is a security one. The Home button is the one way a user can be guaranteed to exit any application. If you could make the Home button launch your application instead of the Home screen without the user confirming this change, it would then be very easy to write a malicious application that hijacked a user's phone.

Alternatively, your application could contain a replicate Home Screen that harvested a user's Google account details; not that hard since the source is available. If your application could silently replace the default Home Screen, it would be hard for the user to tell this had happened.

Also, do you really want to override Home? Home is like an emergency escape button. Pressing Home twice will always take a user back to the center panel of the Home Screen, so whatever application they're running, it's easy for a user to get back to somewhere they know. You shouldn't really be overriding this unless you're producing a full Home replacement.

Back button exits the app instead of going back to MainActivity

Remove meta data from second activity

<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="full_package_name.MainActivity" />

Remove it

<activity
android:name=".question.QuestionActivity"></activity>

is enough

and make sure while you write startActivity(intent) you do not add finish() after it



Related Topics



Leave a reply



Submit