1

Following is my use case:

User click on App icon -> MainActivity ->FirstActivity-> SecondActivity

SecondActivity has three fragments i.e. F1, F2, F3

Suppose user is on F1 in SecondActivity, now if user minimizes the app by pressing home button, and then re-launches the app not using App icon but by selecting from the list which gets displayed by long press (don't know the technical name of that )

I have already explored most of the questions and even tried following approach

How to make an android app return to the last open activity when relaunched?

(but this seems to work, in case when i want to re-launch my app from app icon i.e. from launcher):

Following is my activity and fragment lifecycle method invocation logs:

When i minimize the app:

  • E/SecondActivity﹕ onPause called
  • E/F1﹕ On Pause
  • E/F1﹕ OnSaveInstanceState
  • E/SecondActivity onStop called

On re-open app:

  • E/FirstActivity﹕ onRestart called
  • E/FirstActivity﹕ onStart called
  • E/FirstActivity﹕ onResume called
  • E/SecondActivity﹕ onDestroy called
  • E/F1﹕ On Detach
  • E/FirstActivity﹕ onPause called
  • E/FirstActivity﹕ onStop called

As per my understanding this should work like following:

  • onPause of SecondActivity, I need to save the current visible fragment in shared pref
  • onResume of SecondActivity, I need to pull the fragment name and display that using fragmentManager

But after re-opening app my SecondActivity onResume doesn't gets invoked, instead FirstActivity's onRestart gets called and onDestroy of SecondActivity gets called (as shown above)

After trying lot of options and reading several SO solutions, I am unable to sort this out.

Please suggest, how to handle this, thanks in advance.

Edit:

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:name=".controller.ApplicationLoader"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:alwaysRetainTaskState="true"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SplashActivity"
            android:label="@string/app_name"
            android:noHistory="true"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".RegistrationActivity"
            android:label="@string/title_activity_registration"
            android:noHistory="true"
            android:screenOrientation="portrait"/>
        <activity
            android:name=".HomePageActivity"
            android:label="@string/title_activity_home_page"
            android:noHistory="true"
            android:screenOrientation="portrait">

            <!--
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            -->
        </activity>

        <service
            android:name=".services.BgService"
            android:exported="false"
            android:process=":remote" >
            <intent-filter>
                <action android:name="com.example.bgapp.BackgroundService" />
            </intent-filter>
        </service>

        <receiver android:name=".services.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <activity android:name=".ProductOverviewActivity"
            android:screenOrientation="portrait">
        </activity>

        <receiver android:name=".receivers.SmsReceiver"
            android:screenOrientation="portrait">
            <intent-filter android:priority="99999">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>

        </receiver>

    <!-- Intent service -->
        <service
            android:name=".services.WService"
            android:exported="false" />

        <service android:name=".services.VerifyService" android:exported="false"/>


    </application>

</manifest>
Community
  • 1
  • 1
user909090
  • 53
  • 4
  • How are you launching the app for the **first time**? From an IDE (like Android Studio, Eclipse, IntelliJ) or from the installer or by selecting the icon from the HOME screen or the list of available apps? – David Wasser Oct 01 '15 at 16:12
  • Thanks for responding. I have verified both running via Android Studio and generating and installing app directly. Even i tried debugging but nothing concrete I am getting. Thanks in advance. – user909090 Oct 01 '15 at 17:48
  • As requested, I have attached the manifest. Here SplashActivity is the MainActivity, ProductOverviewActivity is the FirstActivity and RegistrationActivity is the SecondActivity. RegistrationActivity has 3 fragments, and I want to resume on RegistrationActivity, but it is going to ProductOverviewActivity – user909090 Oct 01 '15 at 19:23

1 Answers1

2

Your problem is your use of:

 android:noHistory="true"

on RegistrationActivity. As soon as the user presses HOME, that RegistrationActivity is finished. Here's a quote from the documentation on noHistory:

A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.

David Wasser
  • 93,459
  • 16
  • 209
  • 274