Home » Android » android – Issues with Translucent Theme

android – Issues with Translucent Theme

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have an app that has two activities.
The first one is presented with a single button that opens the second one.

Here is the Manifiest definition for the first one:

<activity
    android:name="com.example.buttonexample.MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Second activity:

<activity
    android:name="com.example.buttonexample.MainActivity2"
    android:label="@string/title_activity_main_activity2" android:theme="@android:style/Theme.Translucent">
</activity>

Here is how I launch the second activity (via OnClickListener for a button on the first activity):

public void startSecondActivityClick(View v) {
   Intent startActivity2 = new Intent(this, MainActivity2.class);
   startActivity(startActivity2);
}

This works fine, however when I background the app by hitting home and the foreground the app. I’m noticing that the first activity is continually creating/destroying itself. I verified this by putting some code in the onDestory method to increment a static int:

private static int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    count++;

}

protected void onDestroy() {
    super.onDestroy();
    Log.i("MainActivity", String.format("Destroyed, %d", count));
}

I’ve also noticed that removing the translucent theme seems to fix this. My question is is there a way to translucent or something similar but also have it not restart? Also, I’m curious why this happens at all. I’m testing this on 4.0.1 ICS on a galaxy SIII.

How to&Answers:

Ok after some digging I was able to figure out why this is happening. Someone had turned on one of the developer options, “do not keep activities.”. After turning this off this stopped happening. I suspect this wouldn’t happen in production too often as most people probably don’t have that setting on. You can find this under settings -> “developer options” on most phones.