1

When running the trivial test activity below on a Samsung Galaxy Tab 10.1" (Android 3.1), I see a short flash of the home screen background, before TestActivity2 is started.

This flash is not seen
- when running without the FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
- when running on a Galaxy Tab 7" running Android 2.3

Any idea what is causing this flash and how I can avoid it?

public class TestActivity1 extends Activity
{
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    TextView t = new TextView(this);
    t.setText("TestActivity1");
    t.setOnClickListener(new View.OnClickListener()
    {

      @Override
      public void onClick(View v)
      {
        //start TestActivity2
        Intent intent = new Intent(getApplicationContext(), TestActivity2.class);           
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        startActivity(intent);          
      }
    });
    setContentView(t);
  }

}

When comparing the LogCat of a test-run with and without the FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET, I noticed the following difference.

With FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET

... INFO/SurfaceFlinger(223): id=71 Removed com.zappware.test/com.zappware.test.TestActivity1 idx=2 Map Size=3

Without FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET

... INFO/SurfaceFlinger(223): id=75 Removed com.zappware.test/com.zappware.test.TestActivity1 idx=3 Map Size=3

Maybe this can help?

Marc Van Daele
  • 2,856
  • 1
  • 26
  • 52
  • The same thing happens when re-starting TestActivity1 with Intent.FLAG_ACTIVITY_CLEAR_TOP – Marc Van Daele Jan 02 '12 at 13:35
  • Why are you using the flag in the first place? Have you tried making an XML file and setting the content view to that? – sebastianf182 Jan 02 '12 at 16:57
  • @sfratini : I'm using this flag when I'm starting an activity with some detailed information. When the user presses home, I do not want to return to this detail page but rather to the more general overview activity. – Marc Van Daele Jan 03 '12 at 08:14
  • @sfratini I've been looking for a mapping between FLAG_ACTIVITY constants and values in the Manifest but couldn't find one (any pointers?): I assume that FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET maps to android:finishOnTaskLaunch (and what is the equivalent for FLAG_ACTIVITY_CLEAR_TOP?). If I use android:finishOnTaskLaunch from the manifest and don't set it in code, there is no flash! This is interesting but of course I would like to understand why they behave differently. – Marc Van Daele Jan 03 '12 at 08:15
  • I also noted that the flashing background image can be avoided by adding a call to overridePendingTransition(0,0) after calling startActivity. Again interesting, but any idea why this makes a difference? – Marc Van Daele Jan 03 '12 at 08:17

1 Answers1

0

Well, you are cleaning the stack so maybe that's why its happening. May I suggest overring the back button behavior?

http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html

Edit:

I was thinking on something like this. Starting with 2.0 you can do this:

@Override
public void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), MyActivity.class);
startActivity(intent);
}
sebastianf182
  • 9,844
  • 3
  • 34
  • 66
  • It might indeed be stack related. However... when using FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET, the stack isn't cleaned. When I go from Activity1 to Activity2, Activity1 remains on the stack. The flag should only have effect when pressing HOME when Activity2 is active. The same holds for the second case: when I navigate from Activity1 to Activity1 using FLAG_ACTIVITY_CLEAR_TOP: also here Activity1 remains on the stack (only the activities on top of it are removed so the stack should never be empty) – Marc Van Daele Jan 04 '12 at 08:54
  • The purpose of using FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET is: suppose you start the activities A-B-C-D and C and D are started with FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET. When you press HOME, the stack is trimmed to A-B. I'm happy with your suggestion (and I appreciate your help) to override the back button behavior but can you elaborate a bit on why this might help? – Marc Van Daele Jan 04 '12 at 09:00
  • Well, you are saying that you want to make the user go back to a particular Activity instead of the one thats before on the stack. Overrring the back button behavior you can do that only on that Activity and keep the normal behavior for all the activities. See my edit. – sebastianf182 Jan 04 '12 at 12:55
  • It's OK for me if I have A-B-C-D and back is pressed to end up in C. What I want is that after A-B-C-D-HOME, the stack is back to A-B so I do think FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET is a better choice here – Marc Van Daele Jan 04 '12 at 13:30
  • Oh I see. I understand now. Yeah, I think is better that way. – sebastianf182 Jan 04 '12 at 13:40