Introduction to the difference between Android and Back keys

  • 2020-06-23 01:55:19
  • OfStack

1: Differences between Home key listening and Back key listening in Android:

(1). In Android, when the Home key is pressed, the Activity in Stop foreground, i.e. Activity, is set to the stop state [onStop()], instead of the destroy state [onDestory()] by default. If the Activity is started again, instead of calling the onCreate() method, the onSavedInstanceState method is called. It starts from onRestart() -ES17en () -onResume ().
(2). When the back key is pressed, the back key defaults to activity of finish foreground, that is, until the state of activity is onDestory. If the activity is started again, the onCreate method will not be called.
In summary: pressing the Home key returns to the desktop, while back returns the first activity.

The realization method of Back key is not redundant here, mainly explains the realization method of Home key 1. Here are some references I found to think for myself. Please give your Suggestions.
Implementation method of Home key monitoring:
(1). Register the broadcast in onResum and cancel the broadcast in OnPause.
(2). Intercept Intent.ACTION_CLOSE_SYSTEM_DIALOGS this Action in the broadcast, and determine whether long press or click Home key by obtaining the Reason field.
The code is as follows:

(1).Home key listening encapsulation class:


package com.scd.homewatcher.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

/**
 * Home Key listening encapsulation 
 * 
 * @author way
 * 
 */
public class HomeWatcher {

  //  Callback interface 
  public interface OnHomePressedListener {

    public void onHomePressed();

    public void onHomeLongPressed();
  }

  private static final String TAG = "HomeWatcher";
  /**  context  */
  private Context mContext;
  /**  The filter  */
  private IntentFilter mFilter;
  /**  interface  */
  private OnHomePressedListener mListener;
  /**  Broadcast receiver  */
  private InnerRecevier mRecevier;

  public HomeWatcher(Context context) {
    mContext = context;
    mRecevier = new InnerRecevier();
    mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
  }

  /**
   *  Set up to monitor 
   * 
   * @param listener
   */
  public void setOnHomePressedListener(OnHomePressedListener listener) {
    mListener = listener;
  }

  /**
   *  Start listening, register for broadcast 
   */
  public void startWatch() {
    if (mRecevier != null) {
      mContext.registerReceiver(mRecevier, mFilter);
    }
  }

  /**
   *  Stop listening and cancel the broadcast 
   */
  public void stopWatch() {
    if (mRecevier != null) {
      mContext.unregisterReceiver(mRecevier);
    }
  }

  /**
   *  Broadcast receiver 
   */
  private 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.i(TAG, "action:" + action + ",reason:" + reason);
          if (mListener != null) {
            if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
              //  Short press home key 
              mListener.onHomePressed();
            } else if (reason
                .equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
              //  Long press home key 
              mListener.onHomeLongPressed();
            }
          }
        }
      }
    }
  }
}

(2). MainActivity class:


package com.scd.homewatcher;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import com.scd.homewatcher.util.HomeWatcher;
import com.scd.homewatcher.util.HomeWatcher.OnHomePressedListener;

public class MainActivity extends Activity implements OnHomePressedListener {
  private HomeWatcher mHomeWatcher;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  }

  @Override
  protected void onResume() {
    mHomeWatcher = new HomeWatcher(this);
    mHomeWatcher.setOnHomePressedListener(this);
    //  Registration of radio 
    mHomeWatcher.startWatch();
    super.onResume();
  }

  @Override
  protected void onPause() {
    mHomeWatcher.setOnHomePressedListener(null);
    //  Cancellation of radio 
    mHomeWatcher.stopWatch();
    super.onPause();
  }

  @Override
  public void onHomePressed() {
    // TODO
    Toast.makeText(this, " Short press Home key , Implement your own logic ", Toast.LENGTH_SHORT).show();

  }

  @Override
  public void onHomeLongPressed() {
    // TODO
    Toast.makeText(this, " Long press Home key , Implement your own logic ", Toast.LENGTH_SHORT).show();

  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
      System.out.println(" Press the back key  onKeyDown()");
      return false;
    } else {
      return super.onKeyDown(keyCode, event);
    }

  }

  @Override
  public void onBackPressed() {
    // super.onBackPressed() Will automatically call finish() methods , Shut down 
    super.onBackPressed();
  }

}


Related articles: