3

I need to know if the following flow is normal: Activity A onPause is called because activity B took the focus, but a few seconds later, when activity B is finished and before onStop & onDestroy of Activity A were called, Activity A (same instance) onResume is called. I have noHistory=true in the activity A defition in the manifest.

I thought that an instance of an activity with noHistory=true will never be returned once the activity has lost it's focus.

galbarm
  • 2,441
  • 3
  • 32
  • 52
  • i guess you have to let it die (full activity lifecyle). one more thing Activity A has to be fully hidden from the screen for the events to get fired. if you dont want for sure just call finish() before you fire of for Activity B. – Samuel Aug 17 '11 at 10:59

3 Answers3

5

The behavior you describe with ActivityA.onResume() being called is not correct. I suspect a typo in your AndroidManifest.xml file. Can you post it and show us?

The timing of onStop() and onDestroy() are a little less defined. Here's an example that works, but onStop() and onDestroy() aren't called until the user hits the back button (but onResume() is never called). If I call finish() after launching ActivityB then they're called on ActivityA earlier.

OUTPUT without finish():

D/HelloAndroidActivity(13013): [HelloAndroidActivity.java:19:onCreate()] onCreate()com.example.hello.HelloAndroidActivity@4055d2e8
D/HelloAndroidActivity(13013): [HelloAndroidActivity.java:45:onStart()] onStart()com.example.hello.HelloAndroidActivity@4055d2e8
D/HelloAndroidActivity(13013): [HelloAndroidActivity.java:39:onResume()] onResume()com.example.hello.HelloAndroidActivity@4055d2e8
NEXT!
D/HelloAndroidActivity(13013): [HelloAndroidActivity.java:51:onPause()] onPause()com.example.hello.HelloAndroidActivity@4055d2e8
D/GoodbyeAndroidActivity(13013): [GoodbyeAndroidActivity.java:16:onCreate()] onCreate()
D/GoodbyeAndroidActivity(13013): [GoodbyeAndroidActivity.java:32:onStart()] onStart()
D/GoodbyeAndroidActivity(13013): [GoodbyeAndroidActivity.java:26:onResume()] onResume()
[BACK]
D/GoodbyeAndroidActivity(13013): [GoodbyeAndroidActivity.java:38:onPause()] onPause()
D/HelloAndroidActivity(13013): [HelloAndroidActivity.java:57:onStop()] onStop()com.example.hello.HelloAndroidActivity@4055d2e8
D/HelloAndroidActivity(13013): [HelloAndroidActivity.java:63:onDestroy()] onDestroy()com.example.hello.HelloAndroidActivity@4055d2e8
D/GoodbyeAndroidActivity(13013): [GoodbyeAndroidActivity.java:44:onStop()] onStop()
D/GoodbyeAndroidActivity(13013): [GoodbyeAndroidActivity.java:50:onDestroy()] onDestroy()

OUTPUT with finish:

D/HelloAndroidActivity(13113): [HelloAndroidActivity.java:19:onCreate()] onCreate()com.example.hello.HelloAndroidActivity@4051b940
D/HelloAndroidActivity(13113): [HelloAndroidActivity.java:45:onStart()] onStart()com.example.hello.HelloAndroidActivity@4051b940
D/HelloAndroidActivity(13113): [HelloAndroidActivity.java:39:onResume()] onResume()com.example.hello.HelloAndroidActivity@4051b940
NEXT!
D/HelloAndroidActivity(13113): [HelloAndroidActivity.java:51:onPause()] onPause()com.example.hello.HelloAndroidActivity@4051b940
D/GoodbyeAndroidActivity(13113): [GoodbyeAndroidActivity.java:16:onCreate()] onCreate()
D/GoodbyeAndroidActivity(13113): [GoodbyeAndroidActivity.java:32:onStart()] onStart()
D/GoodbyeAndroidActivity(13113): [GoodbyeAndroidActivity.java:26:onResume()] onResume()
D/HelloAndroidActivity(13113): [HelloAndroidActivity.java:57:onStop()] onStop()com.example.hello.HelloAndroidActivity@4051b940
D/HelloAndroidActivity(13113): [HelloAndroidActivity.java:63:onDestroy()] onDestroy()com.example.hello.HelloAndroidActivity@4051b940
[BACK]
D/GoodbyeAndroidActivity(13113): [GoodbyeAndroidActivity.java:38:onPause()] onPause()
D/GoodbyeAndroidActivity(13113): [GoodbyeAndroidActivity.java:44:onStop()] onStop()
D/GoodbyeAndroidActivity(13113): [GoodbyeAndroidActivity.java:50:onDestroy()] onDestroy()

HelloAndroidActivity.java:

public class HelloAndroidActivity extends Activity {
    private static final String TAG = "HelloAndroidActivity";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate()" + this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent(HelloAndroidActivity.this,
                        GoodbyeAndroidActivity.class);
                startActivity(i);
                // Uncomment this:
                 finish();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume()" + this);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart()" + this);
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause()" + this);
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG, "onStop()" + this);
    }

    @Override
    public void onDestroy() {
        super.onStop();
        Log.d(TAG, "onDestroy()" + this);
    }

}

GoodbyeAndroidActivity.java:

public class GoodbyeAndroidActivity extends Activity {
    private static final String TAG = "GoodbyeAndroidActivity";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goodbye);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume()");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart()");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause()");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG, "onStop()");
    }

    @Override
    public void onDestroy() {
        super.onStop();
        Log.d(TAG, "onDestroy()");
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:id="@+id/hello_text"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
        <Button  
        android:id="@+id/button"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="NEXT!"
        />
    </LinearLayout>

goodbye.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:id="@+id/hello_text"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Goodbye!!!"
    />
</LinearLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.hello"
          android:versionCode="1"
          android:versionName="1.0">
        <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10"/>

        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name="com.example.hello.HelloAndroidActivity"
                      android:label="@string/app_name"   android:noHistory="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name="com.example.hello.GoodbyeAndroidActivity">
            </activity>

        </application>
    </manifest>
Jason
  • 1,086
  • 5
  • 10
  • Thanks for the response. This is the way the activity is defined: – galbarm Aug 18 '11 at 05:47
  • I have a class BaseActivity which extends Activity and from which LoginActivity is derived. In the base class I send a print to the log the way you did in your code so this is the way I see that I return to LoginActivity.onResume after being in another activity. – galbarm Aug 18 '11 at 05:50
  • Very strange. I copied and pasted your attributes into my application and it worked fine. Questions that come to mind: What's your minSdkVersion and targetSdkVersion? Have you been able to reproduce this on multiple devices? Can you get the example I posted here to work in your environment? – Jason Aug 18 '11 at 13:22
  • I'm working with minSdkVersion=8 and also using this sdk. Something I haven't mentioned is that the problem does not reproduce on every run but only in about 10% of the times. As kind of a workaround I'm now calling finish() from the LoginActivity. Since making this change, the behavior was not reproduced. – galbarm Aug 18 '11 at 15:26
  • Is this in the emulator or on an actual device? If a device... which one? – Jason Aug 18 '11 at 16:04
1

It sounds like @Sam Quest has your answer. Call onFinish() before launching a new activity. If it happened due to user navigation then your activity should be finished but I don't know if there is any guarantee when. If your LoginActivity is creating the activity then calling onFinish() sounds like the right thing to do, not a workaround.

Edwin Evans
  • 2,726
  • 5
  • 34
  • 47
  • It says "Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen". This is not the behavior I see. In this case it is important for me to understand why it doesn't work as expected, the way I try to do it. Not to find other ways to do it. – galbarm Sep 19 '11 at 06:48
1

use on activity result. Then your problem will solve.

harish
  • 255
  • 1
  • 3
  • 13