Activitygroup Example

ActivityGroup Example

david here is a an example I found useful:

http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html

Android: ActivityGroup tutorial with example

Are you trying to extend ActivityGroup in your project with specific requirements? Here is a tutorial with a TabActivity and ActivityGroup.

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

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

Can I use NativeActivity with ActivityGroup?

This time I found a workaround for it, and works fine. but just for the ndk demos.

on your ActivityGroup sub class onCreate method, write the following code.

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LocalActivityManager lam = getLocalActivityManager();

Intent intent = new Intent();
intent.setClass(this, TeapotNativeActivity.class);

Window window = lam.startActivity("xxx", intent);

// reflect call "willYouTakeTheSurface"
NativeActivity callback = JavaCalls.callMethod(window.getDecorView(), "willYouTakeTheSurface");
if (callback != null) {
window.takeSurface(null);
getWindow().takeSurface(callback);
getWindow().takeInputQueue(callback);
}

setContentView(window.getDecorView());

}

Android replace ActivityGroup with Fragments / FragmentsManager

In my case I used FragmentManager V4 from the android support library and tabhost with Fragments and did the things to work. If someone needs an example you can find it in the Android Developers blog or just search for Android Fragments Example on Google and you will find something useful. : )



Related Topics



Leave a reply



Submit