Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I gain some efficiency by declaring appropriate members "static" in Android Activity

If an Activity is a singleton in practice, I think I can gain some efficiency by declaring appropriate members "static", with zero risk. Yes?

like image 214
DJC Avatar asked Aug 03 '10 18:08

DJC


2 Answers

One thing please DO NOT use singleTask or singleInstance for this purpose. The activity launch flags are there to control how activity stacks behave. They have visible impact on the user interaction with your activity (making it non-standard). Those modes are intended to be used when you want that kind of user interaction, they should NOT be used to change the implementation details of your app.

like image 45
hackbod Avatar answered Oct 05 '22 07:10

hackbod


The Android documentation says -

there's never more than one instance of a "singleTask" or "singleInstance" activity, so that instance is expected to handle all new intents.

This means you can use static members.

Besides, a standard or singleTop should have thread-safe static members only. Suppose the current activity stack is A-B-C-D. If the arriving intent is for an activity of type B which is in "standard" or "singleTop" mode. A new instance of B would be launched as (since B is not at the top of the stack), so the resulting stack would be A-B-C-D-B.

like image 92
ankitjaininfo Avatar answered Oct 05 '22 07:10

ankitjaininfo