I am building a android application with two Activities utilizing the Action Bar (https://github.com/johannilsson/android-actionbar as i am targeting Android 2.2).
It has a number of activities. There is a "Home" Activity, called Feed, and another activity called "Settings".
The problem i am having is that using the createIntent function that is given in the sample for the action bar i am using, the Activity still get destroyed rather than resumed when the user taps the home button to return to the Feed activity.
With a bit of debugging i found that it is getting destroyed, not when the Activity is first paused and stopped, but when the request for it to resume happens.
public static Intent createIntent(Context context)
{
Intent i = new Intent(context, Feed.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return i;
}
This is a major annoyance, and considerably slows down the application as it has to reload its data for the feed (which is cached, but its not instant to get the data reloaded).
So how can i avoid this behavior? and why is this happening, as i believe the extra flag there should stop this behavior.
You want to use:
FLAG_ACTIVITY_REORDER_TO_FRONT
Quote from Android docs:
If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.
For example, consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B. This flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified.
public static Intent createIntent(Context context)
{
Intent i = new Intent(context, Feed.class);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
return i;
}
Depending on which class is launching what, you might be having an issue where a new instance of your activity is being launched and the old one is being destroyed. This is what the documentation says about FLAG_ACTIVITY_CLEAR_TOP
:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
Notice that it says "if the activity being launched is already running in the current task..." So I'm not sure, but this would make sense if your debugging has shown you that the activity is also being destroyed.
When debugging, are you looking at the "id" numbers for your class objects? Set a breakpoint in both the onCreate()
and onDestroy()
methods of your class and look at the id of the class inside each one. If they are different, then you know you have two different instances of your activity.
As a side note/question, how are you "targeting" 2.2 if the ActionBar
wasn't made available until 3.0?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With