Home button listener
You do not need to catch Home button. If user press Home and some other Activity comes to foreground, your app goes to background and onPause()
is called in your current Activity. You may override that function to clean search string or anything you need.
UPDATE:
More clean solution is to use flag FLAG_ACTIVITY_NO_HISTORY when starting that critical activity. So, when your activity goes to background system will close it properly for you.
Action Bar's onClick listener for the Home button
Fixed: no need to use a setOnMenuItemClickListener
.
Just pressing the button, it creates and launches the activity through the intent.
Thanks a lot everybody for your help!
Home button click event handling android
On older Android version this is working. But Android changed this, because they say "Home Button should stay Home Button" and they don't want that anybody override the Home Button. And because of this reason your code is not working anymore.
If you want to do something when the home button is pressed, then do this in the onPause method.
how to stop service when click home button?
Use this code for home button press event :
HomeWatcher mHomeWatcher = new HomeWatcher(this);
mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
@Override
public void onHomePressed() {
// do something here...
}
@Override
public void onHomeLongPressed() {
}
});
mHomeWatcher.startWatch();
HomeWatcher.class :
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
public class HomeWatcher {
static final String TAG = "hg";
private Context mContext;
private IntentFilter mFilter;
private OnHomePressedListener mListener;
private InnerRecevier mRecevier;
public HomeWatcher(Context context) {
mContext = context;
mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
}
public void setOnHomePressedListener(OnHomePressedListener listener) {
mListener = listener;
mRecevier = new InnerRecevier();
}
public void startWatch() {
if (mRecevier != null) {
mContext.registerReceiver(mRecevier, mFilter);
}
}
public void stopWatch() {
if (mRecevier != null) {
mContext.unregisterReceiver(mRecevier);
}
}
class InnerRecevier extends BroadcastReceiver {
final String SYSTEM_DIALOG_REASON_KEY = "reason";
final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if (reason != null) {
Log.e(TAG, "action:" + action + ",reason:" + reason);
if (mListener != null) {
if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
mListener.onHomePressed();
} else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
mListener.onHomeLongPressed();
}
}
}
}
}
}
}
OnHomePressedListener.class :
public interface OnHomePressedListener {
public void onHomePressed();
public void onHomeLongPressed();
}
Android home button listener
Anyone know where to put method that we want to call 1st when the app is re-opened
You have to put it in the onResume()
method.
Take a look at the Activity lifecycle:
Related Topics
How to Solve Render Problem Path.Op() Not Supported
Finish an Activity After a Time Period
Why Does Flag_Activity_Clear_Top Not Work
A Pre-Populated Database Does Not Work at API 28 Throws "No Such Table" Exception
Vectordrawablecompat Resources$Notfoundexception on Kitkat and Below
Provide the Caller Id for Incoming Call from My Own App
How to Use Std::Stoul and Std::Stoull in Android
Changed Jquery Mobile's Default Icon Set, Work on Browser, Failed on Device
How Open New Activity Clicking an Item in Listview
Using Android Google Maps V2 with Custom View or Imageview
Clicking on Notification Is Not Starting Intended Activity
How to Make a Dialog Slide from Bottom to Middle of Screen in Android
How to Handle Empty Response Body with Retrofit 2
How to Bring a Background Task to The Front Below Honeycomb
How to Integrate Opencv 4.0 into a Pure C++ Android Ndk Project