即使它具有noHistory = true,也返回活动

gal*_*arm 3 android activity-stack android-activity

我需要知道以下流程是否正常:活动A onPause被调用,因为活动B占据了焦点,但几秒钟后,当活动B完成并且在调用活动A的onStop和onDestroy之前,活动A(相同的实例) )onResume被调用.我在清单中的活动A defition中有noHistory = true.

我认为,一旦活动失去焦点,就永远不会返回具有noHistory = true的活动的实例.

Jas*_*son 5

您通过ActivityA.onResume()调用描述的行为不正确.我怀疑你的AndroidManifest.xml文件中有拼写错误.你能发布并向我们展示吗?

时间onStop()和时间onDestroy()稍微不那么明确.下面是工作的例子,但onStop()onDestroy()没有叫,直到用户点击后退按钮(但onResume()不会被调用).如果我finish()在启动ActivityB之后调用它们,那么它们会在之前的ActivityA上调用.

OUTPUT没有完成():

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()
Run Code Online (Sandbox Code Playgroud)

完成输出:

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()
Run Code Online (Sandbox Code Playgroud)

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

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()");
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)