Question

I want to open many views under the same tab in Android. In other words, i have a tab host containing many tabs. One of these tabs has a list view as content. When a list item is clicked, i want it to open a new view under the same tab. I did a little Google researches and i found that i must use android fragment, but i am not sure of this. Do you think that the use of Fragment is the best solution or do you have any other idea? Thank you in advance.

Was it helpful?

Solution

Fragment and FragmentManager are the recommended path forward since Honeycomb. You'll want to use the compatibility library (http://developer.android.com/sdk/compatibility-library.html) if you intend to target Gingerbread or earlier devices.

You'll notice that ActivityGroup has been deprecated. That doesn't mean you can't use it now, but at some point you'll be forced to migrate anyway, so you might as well start now.

OTHER TIPS

Use ActivityGroup class with ViewAnimator and get right activity by ID.

public class YourActivity extends ActivityGroup {

    private Stack<String> ids;
    private LocalActivityManager activityManager;
    private ViewAnimator animator;
    private int serial;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.group);
        ids = new Stack<String>();
        animator = (ViewAnimator) findViewById(R.id.animator);
        activityManager = getLocalActivityManager();
    }

    @Override
    public void startActivity(final Intent intent) {
        String id = "id" + serial++;
        ids.push(id);
        View view = activityManager.startActivity(id, intent).getDecorView();
        animator.addView(view);
        animator.setDisplayedChild(ids.size() - 1);
    }

    @Override
    public boolean onKeyDown(final int keyCode, final KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            int size = ids.size();
            if (size > 0) {
                String topId = ids.pop();
                View view = activityManager.destroyActivity(topId, true).getDecorView();
                animator.removeView(view);

                if (size > 1) {
                    topId = ids.get(size - 2);
                    if (activityManager.getActivity(topId) instanceof ClassA) {
                        ((ClassA) activityManager.getActivity(topId)).onResume();
                    } else if (activityManager.getActivity(topId) instanceof ClassB) {
                        ((ClassB) activityManager.getActivity(topId)).onResume();
                    }
                    return true;
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top