2

My app goes like this: At the beginning i have the LoginActivity which leads to MainActivity which has 3 fragments. In the first fragment i have a listview with 8 items. Each item of the listview is clickable. I suppose that if i want i can go to another activity by clicking one of the listview's item.

My Question is if i can create only one activity instead of making 8 and when i click any of the listview items, to start this activity with different data every time.

By different data i mean : This activity will have the same layout (expandable listview) but some of the titles etc will be different..

alecnash
  • 1,750
  • 1
  • 18
  • 42
fnkbz
  • 1,189
  • 1
  • 12
  • 22
  • @Bill i saw that the question is on hold.. sorry but the question is clear enough and i got three very useful answers.. i marked the one that helped me the most.. if my question is off-topic please feel free to delete it or to close it. – fnkbz Jan 22 '14 at 18:38
  • Please show your code instead of just describing it. "On hold" is temporary. If your question isn't updated and reopened in a few days it will be closed automatically. – Bill the Lizard Jan 22 '14 at 18:40

4 Answers4

1

Here is What you can Try out

1) In Single Activity have this Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/listFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize"
        android:layout_weight="2"
        class="Your package Name" />

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" 
        android:id="@+id/frameLayout">
    </FrameLayout>

</LinearLayout> 

This Layout contains one Fragment which will contain your ListFragment And 2nd one i.e FrameLayout is used to Dynamically add your View you want on Click of ListView item

2) This 2 statement will help you to pop your view which may get overlayed on each other

getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentTransaction.addToBackStack(null);

Hope this could help...

Nitesh Tiwari
  • 4,742
  • 3
  • 28
  • 46
  • Thanks for the answer! it is really helpful.. i think i got the idea! – fnkbz Jan 22 '14 at 10:44
  • either I didn't get the question or your answer is somehow confusing – alecnash Jan 22 '14 at 10:51
  • @alecnash Question asked was... How do get different Fragment on single Activity ?. And Answer: Use A List Fragment and a FrameLayout . ON Each click of List item add new View to the Layout . Also pop it to avoid the overlaying of View – Nitesh Tiwari Jan 22 '14 at 10:55
  • @nitesh Question was if he has to write an activity for every item on the list view which is of course unacceptable. So when the user clicks new Activity opens. Just need to write one and then pass the arguments from the item clicked. That's all – alecnash Jan 22 '14 at 10:59
  • @alecnash I think that nitesh means that i have to create one activity with a list of fragments and when user clicks an item of the listview he goes on the same activity but in a different fragment each time.. – fnkbz Jan 22 '14 at 11:15
  • @greekpoulos Exactly this is What i means...You got it easily – Nitesh Tiwari Jan 22 '14 at 11:20
  • @nitesh So either the question is wrong or the answer. Just pick – alecnash Jan 22 '14 at 11:22
  • @alecnash I dont get it why the answer is wrong can you explain it? i create one activity with a list of fragments and when user clicks an item of the listview he goes on the activity that i have created but in a different fragment of this activity each time.. what's wrong with that? – fnkbz Jan 22 '14 at 11:31
  • if you click on an item in your listView either you swap fragments in the activity that you are in or you can open a new activity. So your question is wrong: "I suppose that if i want i can go to another activity by clicking one of the listview's item". You are talking about new activity not about swapping fragments – alecnash Jan 22 '14 at 11:37
  • @alecnash the part "I suppose that if i want i can go to another activity by clicking one of the listview's item" is not the question it is called hypothesis..i know that this is something that can be done. The question starts bellow this but hypothesis is needed to make the question clear..Try reading more carefully (sorry if the "I Suppose" part tricked you) – fnkbz Jan 22 '14 at 11:48
  • Ok just explain to me what you want to achieve exactly. Open a new activity (I suppose this was your hypothesis, that a new activity can be created) or swap fragments (that is nowhere to be seen in your question)? So either you are not clear or you don't know the difference between activities and fragments – alecnash Jan 22 '14 at 11:52
  • Given the fact that i can create an activity by clicking an item from the listview,My Question is if i can create only one activity instead of making 8 and when i click any of the listview items, to start this activity with different data every time. The Answer was : Create an Activity with a list of fragments and configure the fragments each time.. ok? – fnkbz Jan 22 '14 at 12:01
  • Unfortunately your reputation is too low and stackoverflow doesn't let me chat with you in private. But trust me when I am saying the question isn't clear – alecnash Jan 22 '14 at 12:06
1

Yes, you can pass arguments to a new activity using intents: here explain how.

The intent can contains information for the activity (title, background, etc); the new activity takes and uses it in onCreate method for initialize the layout.

Depending of the use that you want give to the new ativity (if there are one instance of the activity, use it instead of create a new activity, or always create a new instance) you have to configure the activity in the App Manifest: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

Community
  • 1
  • 1
Narkha
  • 1,197
  • 2
  • 12
  • 30
0

First of all you have to read a bit more about android and ListViews. Try googling and getting some tutorials like this: http://www.vogella.com/tutorials/AndroidListView/article.html. Second all you need to do is set an onClickListener on your ListView and pass the right arguments to the ONE activity that you created. Λιγο διαβασμα θελει :)

alecnash
  • 1,750
  • 1
  • 18
  • 42
0

You can maintain a flag in constant class and on click change the value of flag .now at the other activity u can set different data according to ur flag value.. plz make sure ur flag must be static..

Anjali Tripathi
  • 1,477
  • 9
  • 28