Nativescript: Android API17 release: after minimizing "hello world" app and clicking on its icon again, it does not get resumed, but gets restarted

Created on 5 Jan 2017  路  3Comments  路  Source: NativeScript/NativeScript

Problem: Created "hello world" app, tapped the "TAP" button a few times, to see that my apps state has changed, minimized it, clicked its icon again, but instead of resuming activity, it restarted the whole app.

Steps to reproduce:

  1. Create a new "hello world" app with tns create test --ng
  2. Build app in release mode, then install it on your phone.

Tested on 3 phones - API17, API22 and API 25.
This problem only occurs in API17 release mode.
tns --version = 2.4.2
If I minimize the app and resume its activity from the app-stack window, it resumes as it should.

android question

Most helpful comment

Hi @dxshindeo,
I reviewed your case and found that this behavior has been related with Android. When you suspend your app and click on the icon to resume it, the OS will launch the root activity of the app again and will bringing the existing task to the foreground. More about the problem could be found here. As a solution you could add alwaysRetainTaskState and launchMode in your AndroidManifest.xml file, which set up your app to use the same instance of the activity instead of creating new one. For further help you could review the below-given instructions.

  1. open <app_name>/app/App_Resources/Android/AndroidManifest.xml file
  2. add the both properties in the activity tag.
android:alwaysRetainTaskState="true"
android:launchMode="singleTask"

Result:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="__PACKAGE__"
    android:versionCode="1"
    android:versionName="1.0">

    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        />

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="__APILEVEL__"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:name="com.tns.NativeScriptApplication"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.tns.NativeScriptActivity"
            android:label="@string/title_activity_kimera"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@style/LaunchScreenTheme"
            android:alwaysRetainTaskState="true"
            android:launchMode="singleTask">

            <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.tns.ErrorReportActivity"/>
    </application>
</manifest>

Hope this helps.

Regards,
@tsonevn

All 3 comments

Hi @dxshindeo,
I reviewed your case and found that this behavior has been related with Android. When you suspend your app and click on the icon to resume it, the OS will launch the root activity of the app again and will bringing the existing task to the foreground. More about the problem could be found here. As a solution you could add alwaysRetainTaskState and launchMode in your AndroidManifest.xml file, which set up your app to use the same instance of the activity instead of creating new one. For further help you could review the below-given instructions.

  1. open <app_name>/app/App_Resources/Android/AndroidManifest.xml file
  2. add the both properties in the activity tag.
android:alwaysRetainTaskState="true"
android:launchMode="singleTask"

Result:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="__PACKAGE__"
    android:versionCode="1"
    android:versionName="1.0">

    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        />

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="__APILEVEL__"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:name="com.tns.NativeScriptApplication"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.tns.NativeScriptActivity"
            android:label="@string/title_activity_kimera"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@style/LaunchScreenTheme"
            android:alwaysRetainTaskState="true"
            android:launchMode="singleTask">

            <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.tns.ErrorReportActivity"/>
    </application>
</manifest>

Hope this helps.

Regards,
@tsonevn

Thank you!!! It worked!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings