akshay
akshay

Reputation: 5979

onStop() does not gets called?

I'm trying to get logs for Activity Lifecycle. And I'm facing some weird issue here.

When I use activity's theme as android:theme="@style/Theme.AppCompat.Light.NoActionBar and go to next acitivty. onPuase() and onStop() gets called.

But, when I use android:theme="@style/AppTheme, onPause() gets called, but onStop() does not get called.

Are there any events based on Activity Theme?

You can refer the code below.

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" />

TestActivity

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" />

Logs:

 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

Upvotes: 5

Views: 1912

Answers (3)

gopalanrc
gopalanrc

Reputation: 274

Check if you have set the windowIsTranslucent to true for your 2nd Activity. If set, then onStop() will not be called on your 1st Activity.

Upvotes: 0

akshay
akshay

Reputation: 5979

<item name="android:windowIsTranslucent">true</item> does not make changes in activity's lifecycle.

I don't know what was the issue in my case. Restarting the studio resolved the issue.

Upvotes: 0

Tomasz Czura
Tomasz Czura

Reputation: 2434

Your second activity is translucent - it means, that first activity is still visible, so onStop() will not be called. This is very similar to showing dialog - onPause() is called because activity is not in foreground, but is still visible to user - so no onStop() calls

Upvotes: 6

Related Questions