返回活动,即使它没有历史记录 = true

发布于 2024-11-29 21:47:02 字数 232 浏览 1 评论 0原文

我需要知道以下流程是否正常: 活动 A onPause 被调用,因为活动 B 获得了焦点,但几秒钟后,当活动 B 完成且在 onStop 之前,活动 A 被调用。 Activity A 的 onDestroy 被调用,Activity A(同一实例)的 onResume 被调用。 我在清单中的活动 A 定义中没有 noHistory=true 。

我认为一旦活动失去焦点, noHistory=true 的活动实例将永远不会被返回。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风月客 2024-12-06 21:47:02

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

onStop()onDestroy() 的时间定义较少。下面是一个有效的示例,但在用户点击后退按钮之前不会调用 onStop()onDestroy()(但 onResume() > 从未被调用)。如果我在启动 ActivityB 后调用 finish() ,那么它们会提前在 ActivityA 上调用。

不带 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()

带完成的输出:

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>

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>
药祭#氼 2024-12-06 21:47:02

听起来@Sam Quest 有你的答案。在启动新活动之前调用 onFinish()。如果由于用户导航而发生这种情况,那么您的活动应该完成,但我不知道是否有任何保证何时完成。如果您的 LoginActivity 正在创建活动,那么调用 onFinish() 听起来像是正确的做法,而不是解决方法。

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.

烟织青萝梦 2024-12-06 21:47:02

用于活动结果。那么你的问题就会解决。

use on activity result. Then your problem will solve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文