2

We have Android native multiple tab barcode application which contains barcode field for Bluetooth device to enter the barcode. When lunch this mobile, it first have one popup to let user to select the tab. The problem is that every time when the bluetooth device is connected or disconnected to the mobile, the application always get re-lunched with this popup. How can we stop mobile to re-start when the bluetooth device is connected or disconnected?

I'm attaching the AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mentor.med.mobile"
    android:versionCode="1"
    android:versionName="1.0.1" >
    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />
        <uses-permission android:name="android.permission.CAMERA" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/med"
        android:label="@string/app_name"
        android:screenOrientation="sensorPortrait"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.pdx.MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:windowSoftInputMode="adjustPan"
            android:launchMode="singleTop"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="http" android:host="pdx.com" android:path="/view" />
            </intent-filter>
        </activity>            
        <activity
            android:name="com.google.zxing.client.android.CaptureActivity"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:windowSoftInputMode="stateAlwaysHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.zxing.client.android.SCAN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

1 Answers1

1

The application probably registers a broadcast receiver, which listens for bluetooth devices being connected. That receiver then launches the Activity with a popup. You have a few options:

One solution could be to remove the entry in the manifest file which registers the intent filter to a receiver (it could be an activity). But obviously then the app will never launch automatically when Bluetooth devices are connected.

Alternatively, you probably want to change the logic of the receiver so it checks if the activity is already open before re-opening it. One way would be to do this in the Activity itself: depending on how you want the application to behave, you may need to change launchMode="singleTop", and use override methods such as isFinishing(), onNewIntent() to determine if the application is restarting and why.

Jodes
  • 14,118
  • 26
  • 97
  • 156
  • Thanks Jodes. I tried launchMode="singleTop" and it didn't work. I noticed that every time when the bluetooth is connected or disconnected, the application always go this cycle: onPause, onStop, onDestroy, onCreate, onStart, onResume. Basically this bluetooth devive is used as barcode scanner, its function like keyboard. I like to know how to keep this application running without going through these activity cycles, when the bluetooth is connected or disconnected, just like the keyboard is on or dismissed. – user3547115 Sep 09 '14 at 06:15
  • I think you are trying to fight the system, and instead, your activity should handle lifecycle events (`onPause()` etc) by saving state, and restoring on `onResume()` etc. Then all you need to do is use: 1. saved state and 2. the intents received, to determine how or why the activity is started/restarted, and behave appropriately. Another reason for doing this is it's standard practice so that your application will respond properly to orientation changes (or other "configuration" changes), as these would also cause your activity to go through lifecycle changes, which you can't avoid. – Jodes Sep 09 '14 at 10:13