Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android intent filter restart Activity?

I'm developping an app that communicates with usb devices when attached to tablet. To avoid the user to accept android to access the device, I set an intent filter :

<intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            <action android:name="android.hardware.usb.action.USB" />
        </intent-filter>
        <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/usb_device_filter" />

My question is, having this intent filter, the "onCreate" method gets called each time I plug a device and another activity is launched, without the intent filter, it's called only once. Here is the "onCreate" method :

@Override   
protected void onCreate(Bundle savedInstanceState) {    
    try {   
        super.onCreate(savedInstanceState);         
        setContentView(R.layout.activity_msp430_hid);               //define activity layout
        setVersionToTitle();    
        btnSend = (Button) findViewById(R.id.btnSend);      //Send button
        btnSend.setOnClickListener(this);                   //Listener for Send Button
        btnSend.setEnabled(true);
        btnSelectHIDDevice = (Button) findViewById(R.id.btnSelectHIDDevice);    //Select HID Device button
        btnSelectHIDDevice.setOnClickListener(this);        //Listener for Select HID Device button
        btnClear = (Button) findViewById(R.id.btnClear);    //Clear button
        btnClear.setOnClickListener(this);                  //Listener for Clear button
        edtxtHidInput = (EditText) findViewById(R.id.edtxtHidInput);    //User editable text area for sending information to attached device        
        log_txt = (EditText) findViewById(R.id.log_txt);    //Text area for displaying information      
        mLog("Initialized\nPlease select your USB HID device");         
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);    
        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);  //Get USB permission intent for broadcast
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        registerReceiver(mUsbReceiver, filter);             //Register broadcast receiver
        edtxtHidInput.setText("Enter Text Here");   
        uiHandler.postDelayed(runnable, 100);               //Start runnable after 100ms

    } catch (Exception e) {
        Log.e("Init", "Initialization error", e);

    }   
}

The broadcast receiver :

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                setDevice(intent);
            }
        }
        //device attached
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            synchronized (this) {
                setDevice(intent);      //Connect to the selected device
            }
            if (device == null) {
                mLog("device connected");
            }
        }
        //device detached
        if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            if (device != null) {
                device = null;
                //btnSend.setEnabled(false);
            }
            mLog("device disconnected");
        }
    }

The "setDevice" method that simply connects to the device :

private void setDevice(Intent intent) {
    device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    if (device != null && intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
        mLog("Selected device VID:" + Integer.toHexString(device.getVendorId()) + " PID:" + Integer.toHexString(device.getProductId()));
        connection = mUsbManager.openDevice(device);        //Connect to device
        intf = device.getInterface(0);

Why ?

like image 496
Romain Avatar asked Mar 22 '16 14:03

Romain


1 Answers

Apparently, this is the default behavior. Check android:launchMode here.

Default launchMode is standard, which means that every time you attach an USB device, a new Activity instance is created. If you set singleTop, onNewIntent() is called and your activity is re-used, if it's on the top of the stack. Otherwise it'll create a new one.

You can also use singleTask or singleInstance but Google discourages this in most cases. But sometimes it could be the right solution for a problem.

like image 67
Jakub Turcovsky Avatar answered Oct 19 '22 22:10

Jakub Turcovsky