我需要知道以下流程是否正常: Activity A onPause 被调用是因为 Activity B 获得了焦点,但是几秒钟后,当 Activity B 完成并且在调用 Activity A 的 onStop 和 onDestroy 之前,Activity A(相同的实例) onResume 被调用。我在清单中的活动 A 定义中有 noHistory=true 。
我认为一旦活动失去焦点,将永远不会返回 noHistory=true 的活动实例。
我需要知道以下流程是否正常: Activity A onPause 被调用是因为 Activity B 获得了焦点,但是几秒钟后,当 Activity B 完成并且在调用 Activity A 的 onStop 和 onDestroy 之前,Activity A(相同的实例) onResume 被调用。我在清单中的活动 A 定义中有 noHistory=true 。
我认为一旦活动失去焦点,将永远不会返回 noHistory=true 的活动实例。
您描述的ActivityA.onResume()
被调用行为是不正确的。我怀疑您的 AndroidManifest.xml 文件中有错字。你能把它贴出来给我们看吗?
onStop()
和的时间onDestroy()
不太明确。这是一个有效的示例,但在用户点击后退按钮之前不会被调用(但从onStop()
未被调用)。如果我在启动 ActivityB 后打电话,那么他们会更早地在 ActivityA 上被调用。onDestroy()
onResume()
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()
你好AndroidActivity.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);
}
}
再见AndroidActivity.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()");
}
}
主要的.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>
再见.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>
用于活动结果。那么你的问题就会解决。
听起来@Sam Quest 有你的答案。在启动新活动之前调用 onFinish()。如果它是由于用户导航而发生的,那么您的活动应该完成,但我不知道是否有任何保证。如果您的 LoginActivity 正在创建活动,那么调用 onFinish() 听起来是正确的做法,而不是解决方法。