Skip to content Skip to sidebar Skip to footer

Navigate Between Activities In An Activitygroup

I'm developing an app which contain TabHost ,in one of those tabs i have an ActivityGroup , and from this ActivityGroup, i launch another SubActivity ( let's say that i Launch an A

Solution 1:

I faced a problem similar to this when I first started experimenting with ActivityGroups. The issue is that you need to place your onKeyDown() in your Activity. However, you need the Activity to have a reference to the ActivityGroup. Then, when you press back, just call your own onBack() in the ActivityGroup.

(EDIT) Here's a Sample for you

Below is the stripped down ActivityGroup code that handles navigation and history in my apps. It has been adjusted on the fly, so there might be an error. Notice a couple of finer points.

publicclassMyGroupextendsActivityGroup
{
/** Static Reference to this Group. */static MyGroup instance;
/** Keeps Track of the History as a Stack. */private ArrayList<View> myActivityHistory;

    @OverrideprotectedvoidonCreate(final Bundle savedInstanceState)
    {//Call the Base Implementationsuper.onCreate(savedInstanceState);

    // Initialize the Activity History
        myActivityHistory = newArrayList<View>();

    // Build the IntentIntent_root=null;
    //Lists the Applications
        _root = newIntent(this, MyActivity.class);
    // Send the Index to the Child Activity
        _root.setAction(Intent.ACTION_VIEW);
    // Forward the Extras, if they are there// Start the root Activity within the Group and get its ViewfinalView_view= getLocalActivityManager().startActivity("App Preferences", _root).getDecorView();
    // Start the History
        addNewLevel(_view);
    }

    /**
     * Gets the instance of the {@link ApplicationGroup} that the child Activity
     * belongs to.
     *
     * @param index
     *  The Group that was passed to the child in the {@link android.content.Intent
     *  Intent} via an Extra (int).
     * @return
     *  <b>ApplicationGroup -</b> The group that this child was assigned to.
     */staticpublic ApplicationGroup getGroup()
    {   if (instance != null)
            return instance;
    }

    /**
     * Allows the Child to replace the {@link ApplicationGroup}'s current
     * {@link android.view.View View} with the specified View. This is
     * intended to be used specifically by the Child.
     *
     * @param withView
     *  The View to use for replacement.
     */publicvoidaddNewLevel(final View withView)
    {//Adds the old one to history
        myActivityHistory.add(withView);
    // Changes this Groups View to the new View.
        setContentView(withView);
    }

    /**
     * Takes the specified {@link android.app.ActivityGroup ActivityGroup} back
     * one step in the History to the previous {@link android.view.View View}.
     */publicvoidback()
    {   Log.d("Group", "Back overridden");
        //If there are more than one screenif (myActivityHistory.size() > 1)
        {   Log.d("Group", "History called");
        // Remove the most recent View
            myActivityHistory.remove(myActivityHistory.size()-1);
        // Change the View back.
            setContentView(myActivityHistory.get(myActivityHistory.size()-1));
        }
    // Otherwise Exitelse
        {   Log.d("Group", "Program finished");
            finish();
        }
    }

}

Next is the pertinent code for the Activity:

public boolean onKeyDown(int keyCode, KeyEvent event)
{//If back was pressedif (keyCode==KeyEvent.KEYCODE_BACK)
    {   MyGroup.getGroup().back();
        returntrue;
    }
    return super.onKeyDown(keyCode, event);
}

Just make sure that you aren't setting the KeyDownListener to anything funny and it should work fine. :) The changes that I made are because I actually have them in an array of Groups (3 at one time). Essentially, just make the Group a Singleton so you can always have the same instance, and keep an array of your Views so that you have a History. Then reference the History when you click Back or when you add a View.

Hope this helps, FuzzicalLogic

Post a Comment for "Navigate Between Activities In An Activitygroup"