tacos_tacos_tacos
tacos_tacos_tacos

Reputation: 10575

Start activity at top of navigation stack in Android

I am implementing the solution for TabActivity with activities in the TabHost as described at http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html. I realize this is sub-optimal but I am already down this road pretty far and have only one remaining hiccup. I have a need to pop to the top of the stack that I am managing (using a list of String for activity ID) and I know that I can get this activity using:

Activity current = getLocalActivityManager().getActivity(mIdList.get(0));

However, I do not know of a method for "showing" current.

I know that I can do this:

Activity current = getLocalActivityManager().getActivity(mIdList.get(1))
current.finish();

And this will have the desired result in the case of there being at least two activities on the stack. However, when there is only one, it fails very badly with an ArrayOutOfBoundsException.

How can I "show" a given activity?

Upvotes: 1

Views: 474

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

Try this way to start an activity which is always at the top in Activity Stack:

List<ActivityManager.RunningTaskInfo> processes1 = activityManager.getRunningTasks(1);
ComponentName componentInfo = processes1.get(0).topActivity;
String classname =processes1.get(0).topActivity.getClassName(); 
String packagename = processes1.get(0).topActivity.getPackageName();
if(classname.equalsIgnoreCase("com.YOUR_PACKAGE_NAME..YOURACTIVITY_NAME"))
    {
        Intent intent24 = new Intent(Intent.ACTION_MAIN).addCategory(
        Intent.CATEGORY_LAUNCHER).setClassName("YOUR_PACKAGE_NAME",
        "com.YOUR_PACKAGE_NAME..YOURACTIVITY_NAME").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        .addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("YOUR_PACKAGE_NAME",
        "com.YOUR_PACKAGE_NAME..YOURACTIVITY_NAME"));
        getApplicationContext().startActivity(intent24);
    }
    else
    {
     //DONE SOMETHING HERE...
    }

Upvotes: 1

Related Questions