3

i am using the following project

https://github.com/akotoe/android-slide-out-menu.git to develop slide out menu application.

How to run different activities in the same view by clicking the list in slide-menu.

for example if i click on item 1 i would like to parse one XML file in a separate activity and add that activity as a child to this parent view.because on each item click i would like to parse a separate XML file and also i would like to represent that parsed data in a separate layout file.so i need an activity to do that and i want that activity to be added as a child to this parent view.

how can i do this can any one help me in doing this.

if i start a new Intent (startactivity) it is navigating to me a different page. where i can't see this parent page.

madan V
  • 844
  • 3
  • 19
  • 40

3 Answers3

3

UI components that can be embedded inside your activity should be derived from Fragment rather than Activity. When converting your child activities into fragments, you will need to override onCreateView instead of onCreate in order to load the fragment's layout.

In your main activity's layout, you can directly insert the fragment you want to show initially, and give that fragment an ID. Then you can use code to replace the fragment with that ID with a different fragment.

This is a good place to get started: http://developer.android.com/guide/components/fragments.html

This is too big a topic for me to cover everything - you really should look at the Android developer resources - but here are some examples.

As I said, you can put the initial fragment directly into your activity layout. "Fragment" is located on the "Layouts" tab of the layout editor. You give that fragment placeholder an "Id" you can use to identify it as well as the "Name" of the fragment class that will be there to start with.

Then when it's time to switch the fragment, you can use code like this:

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment
transaction.replace(R.id.fragment_container, newFragment);

// Commit the transaction
transaction.commit();
j__m
  • 9,392
  • 1
  • 32
  • 56
  • how can i add the fragment to the activity_layer_stack.xml how can i write my parse logic in fragment class – madan V Mar 20 '13 at 05:28
  • the new fragment is overriding the existing fragment how can i remove the old fragment. – madan V Mar 20 '13 at 05:56
  • The transaction.replace() call replaces the old fragment with the new one. Only one fragment can be on-screen with that id. Take some time to study the Android documentation page I linked; it's actually pretty good at explaining the concept of fragments and how the classes work together. – j__m Mar 20 '13 at 06:00
  • any memory issues will be there if i use more fragments on each button click.when the fragment will be deallocated can you please give some clarification on these two issues. – madan V Mar 20 '13 at 07:39
  • When you use transaction.replace() (and then transaction.commit()), Android is no longer referencing your old Fragment. As long as you didn't make any references of your own the old Fragment will be garbage-collected. – j__m Mar 20 '13 at 16:09
2

You can't nest activity in another one. But you can use Fragment instread. When click the item just swith to right Fragment by FragmentManager.

Shuai
  • 342
  • 3
  • 7
0

Check this Answer first

How do I create a header or footer button bar for my Android application


You can have one Master activity in your project,

Say, MainActivity.java

in that main activity write code for the sliding menu

@Override
    public void onCreate(Bundle inState) {
        super.onCreate(inState);

        mMenuDrawer = new MenuDrawerManager(this, MenuDrawer.MENU_DRAG_CONTENT,
                MenuDrawer.MENU_POSITION_RIGHT);

        mMenuDrawer.setContentView(R.layout.appui);
        mMenuDrawer.setMenuView(R.layout.slide_menu_ui);

    }

In this same activity, initialize your menu components and write listeners for them.

Now,

In your every other Activities extends from MainActivity

and you are done.!!

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • how to call that extended class on each list item click – madan V Mar 22 '13 at 04:48
  • You dont need to call extended class on each item click, you just have those click listener in base class and don't need to write in other child classes. That's the power of OOPS @madanV – MKJParekh Mar 22 '13 at 06:28
  • can you please tell me the way to write that click listener to call extended activity – madan V Mar 22 '13 at 06:50
  • @madanV have you checked the ANSWER Link I given, and see this http://pastebin.com/0z34ivNq – MKJParekh Mar 22 '13 at 07:03
  • when i click on menu item it is opening new activity not like closing the slide and overwriting the existing data in the same layout file.if i press back button it is opening the previous activity also how can i solve the problem. – madan V Mar 26 '13 at 07:50