Android: Using Activitygroup to Embed Activities

android: using ActivityGroup to embed activities

Yes, you'd implement an ActivityGroup, which will be the container of your other Activities. When the user clicks one of the buttons, you'd get a reference to the LocalActivityManager, and use it to start, and embed the inner activity. Something like this:

LocalActivityManager mgr = getLocalActivityManager();

Intent i = new Intent(this, SomeActivity.class);

Window w = mgr.startActivity("unique_per_activity_string", i);
View wd = w != null ? w.getDecorView() : null;

if(wd != null) {
mSomeContainer.addView(wd);
}

Note, using this method can be pretty complicated, because unless the focus is just right, pressing the hardware buttons (like the menu button) will only only trigger events on the ActivityGroup instead of the inner Activity. You have to find some way to focus the inner activity after you add it to the container view, at which point the even will happen in the inner activity and propagate to the container activity.

It can be done, I've done it... and it works. It's just a bit more complicated than I think it should be.

Anyway, I got most of this information by looking at the TabHost code, which can be found here

How to embed a MapActivity into another Activity with content on the top and the bottom?

It is simply impossible to achieve it, i must move all the content from Activity A to activity B (map)

Is there any way to embed an activity in ViewGroup? Or to convert an Activity into a View?

I think what you want is called an ActivityGroup, which has been replaced with Fragment in API 11. Here is a tutorial on using a TabActivity, which is an ActivityGroup.

I think you should be very cautious about doing this though... Activities are meant to represent a single screen for a reason. That way the OS can free resources more easily and so performance is improved.

If you are targeting an API higher than 11, (e.g. Android 3.0+) here is the official guide about Fragments.

I assume that Fragments are a much better solution than ActivityGroups, since they are a platform highlight of honeycomb.

What is a non-embedded activity and why doesn't android:fitsSystemWindows work in it?

An embedded Activity is an Activity which is hosted inside a parent Activity. The common example being the TabHost/TabActivity design. In particular, embedded Acitvities reside in the host's LocalActivityManager, which is conceptually similar to the FragmentManager which allows you to display one Activity inside another.

Given this definition, it is easy to understand why only the host (non embedded) Activity can support the fitsSystemWindows attribute, as any embedded Activities are restricted to the area defined by their host.

It is very unlikely you will have accidentally created one.

See: android: using ActivityGroup to embed activities

Embed external Intent in main Activity

Is it possible to load an external Activity within a View inside my running application?

No, sorry, that is not possible.

Navigate Between Activities in an ActivityGroup

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.

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

@Override protected void onCreate(final Bundle savedInstanceState)
{//Call the Base Implementation
super.onCreate(savedInstanceState);

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

// Build the Intent
Intent _root = null;
//Lists the Applications
_root = new Intent(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 View
final View _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.
*/
static public 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.
*/
public void addNewLevel(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}.
*/
public void back()
{ Log.d("Group", "Back overridden");
//If there are more than one screen
if (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 Exit
else
{ Log.d("Group", "Program finished");
finish();
}
}

}

Next is the pertinent code for the Activity:

public boolean onKeyDown(int keyCode, KeyEvent event)
{//If back was pressed
if (keyCode==KeyEvent.KEYCODE_BACK)
{ MyGroup.getGroup().back();
return true;
}
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

Android Activity Group use

as stated ActivityGroup is deprecated. Nowadays Android Developers are using Fragments with FragmentManagers.

They are pretty simple to use and more flexible to handle. If you want to have downwards compatibility you're able to use the compability-library



Related Topics



Leave a reply



Submit