user1114
user1114

Reputation: 1169

Why does an Android App launch in the background when running from Android Studio?

When pressing "Run" in Android Studio 4.0, sometimes my app will launch in the background on the device - a tablet running Android 7.0. Meaning I'll have to go to the "Recent Items" view on the device and press on the app there to open. The app doesn't launch on the foreground as normal.

When this happens, there are no logs shown in the "Run" View Window in Android Studio - even after opening the app from the "Recent Items" view.

I reached out to Google and have not had much success resolving. I was wondering if anyone here has come across this, or has an idea what this might be.

Reproducing on an Emulator, but not on a new empty project.

EDIT: Adding logs - these are grabbed from the device straight using an adb shell logcat command. These problems happen only when the app is already running, and began happening after upgrading to Android Studio 4.0. My app's name is poscosecha and the full package name is com.plantecuador.poscosecha. The device is an RCA 10" Tablet with Android 7.0 (API 24). The error reproduces on an emulator with the same version of Android. It does not occur 100% of the time, but definitely more than 75% if the app is already running.

There are two distinct error cases. In the first, the app moves to the background. In the second, the app shuts down completely (does not appear in background tasks). The first case seems to occur more often if there are changes between runs, while the second occurs more often if there are no changes.

Logcat for the first case:

https://pastebin.com/T1HFU9zb

Logcat for the second case:

https://pastebin.com/qJque7A7

EDIT 2: Adding Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.plantecuador.poscosecha">

    <uses-permission android:name="android.permission.INTERNET" /> <!-- Bluetooth printer permissions -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />


    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.flash" android:required="false" />

    <uses-feature
        android:name="android.hardware.bluetooth_le"
        android:required="false" /> <!-- Access fine & coarse location is used to determine the macaddress & bluetooth address -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:name=".App"
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true"
        tools:ignore="GoogleAppIndexingWarning"
        tools:replace="android:allowBackup">
        <activity android:name=".SignInActivity" android:screenOrientation="sensorPortrait"
            android:configChanges="keyboard|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER"  />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity" android:screenOrientation="sensorPortrait"
            android:configChanges="keyboard|keyboardHidden"/>
    </application>

</manifest>

Upvotes: 9

Views: 1778

Answers (1)

peynir
peynir

Reputation: 68

android:screenOrientation="sensorPortrait" may be causing the issue. If the tablet is on landscape mode, it may deny to go portrait mode without user interaction.

Upvotes: 0

Related Questions