2

Stemming from this article more efficient way of updating UI from service I was wondering if I could take that a step further and implement the following. I may have a misunderstanding of my Apps lifecycle though.

public class MyApplication extends Application {

    private static final String TAG = MyApplication.class.getSimpleName();
    private static Stack<MyActivity> mActivityStack = new Stack<MyActivity>();
    private static String mTopActivity = "none";

    public static void pushActivity(MyActivity activity)
    {
        mActivityStack.push(activity);
        mTopActivity = activity.getClass().getSimpleName();
        Log.i(TAG, "push::"+mTopActivity);
    }

    public static void popActivity()
    {
        Log.i(TAG, "pop::"+mTopActivity);
        mActivityStack.pop();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Log.w(TAG, "low memory!!!");
        Log.w(TAG, "Current::"+mTopActivity);
    }
}

public class MyActivity extends Activity
{
    private static final String TAG = MyActivity.class.getSimpleName();

    public void onCreate(Bundle last)
    {
        super.onCreate(last);
        MyApplication.pushActivity(this);
    }

    public void onDestroy()
    {
        super.onDestroy();
        MyApplication.popActivity();
    }
}

Would the stack be valid during the lifecycle of the application?


As CommonsWare said, this did not work out. Also, it is not a great idea to derive from Activity, because you would then have to also derive listactivity, preferenceactivity, etc. Obviously, I did not think this would solve any problem it was just an experiment in android life cycles.

Patrick
  • 1,717
  • 7
  • 21
  • 28
Tom Fobear
  • 6,729
  • 7
  • 42
  • 74
  • Well on a glance no because you have never actually created an object of the type Stack so mActivityStack will always be null. I would expect a null pointer exception from push/popActivity when this code is run. – Robert Massaioli Mar 12 '11 at 01:42
  • forgot to edit name to MyActivity =x. Also, forgot to instantiate my stack =x. updated and fixed though. – Tom Fobear Mar 12 '11 at 01:45

1 Answers1

1

Would the stack be valid during the lifecycle of the application?

Of course it won't be valid. You assume every activity is created and destroyed in the same sequence. They won't be in many cases (e.g., user presses HOME).

Whatever problem you think you are solving this way, this is not the right solution by any stretch of the imagination.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491