我正在嘗試獲取活動生命週期的日誌。我在這裏面臨一些奇怪的問題。onStop()不會被調用?
當我使用活動的主題爲android:theme="@style/Theme.AppCompat.Light.NoActionBar
並轉到下一個活動。 onPuase()
和onStop()
被調用。
但是,當我使用android:theme="@style/AppTheme
時,onPause()
被調用,但onStop()
不被調用。
是否有任何事件基於活動主題?
您可以參考下面的代碼。
Styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowIsTranslucent">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
的Manifest.xml
<activity
android:name=".activity.TestActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.TestTwoActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme" />
測試活動
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Logger.debug("TestActivity onCreate");
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(TestActivity.this, TestTwoActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
Logger.debug("TestActivity onStart");
super.onStart();
}
@Override
protected void onRestart() {
Logger.debug("TestActivity onRestart");
super.onRestart();
}
@Override
protected void onResume() {
Logger.debug("TestActivity onResume");
super.onResume();
}
@Override
protected void onPause() {
Logger.debug("TestActivity onPause");
super.onPause();
}
@Override
protected void onStop() {
Logger.debug("TestActivity onStop");
super.onStop();
}
@Override
protected void onDestroy() {
Logger.debug("TestActivity onDestroy");
super.onDestroy();
}
}
activity_test
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/bg_blue"
android:text="button" />
`activity_test_two
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:background="@color/bg_blue"
android:text="button" />
日誌:
TestActivity onCreate
TestActivity onStart
TestActivity onResume
activity Button Click
TestActivity onPause
TestTwoActivity onCreate
TestTwoActivity onStart
TestTwoActivity onResume
Backpress
TestTwoActivity onPause
TestActivity onResume
TestTwoActivity onStop
TestTwoActivity onDestroy
從主題中刪除- true
。仍面臨同樣的問題。 –
akshay
你可以發佈你的活動佈局嗎? –
謝謝你的回答Tomasz。 windowIsTranslucent不影響活動的生命週期。我已重新啓動我的系統,它開始工作正常:( – akshay