SW18
SW18

Reputation: 27

Creating my first app, confused about main activity and fragments

I'm very new to this but am very interested in creating my first app.
I managed (somehow) to get the navigation drawer working within my application, mainly due to following a YouTube tutorial.

I have my main activity and 5 other fragment files, one of which is called the 'Home' fragment.
Whenever I press 'Home' on the navigation bar I want this to take the user to the main activity page, which is the page that boots currently when the user first opens the app.
So essentially I want a fragment to appear within the activity on the main screen.
I have no idea how to do this and have already tried looking at the fragment documentation and it confuses me.

Could anyone help please?

Summary:
1. Want to display fragment on home page
2. The only reason why I want to do this is everything I place on my main activity then appears on every single fragment page

Upvotes: 0

Views: 75

Answers (2)

Dheeraj Joshi
Dheeraj Joshi

Reputation: 1582

To add a fragment into a Activity or FramentActivity it requires a Container. That container should be a "Framelayout", which can be included in xml or else you can use the default container for that like "android.R.id.content" to remove or replace a fragment in Activity.

XML FILE

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Framelayout to display Fragments -->
<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ImageView
    android:id="@+id/imagenext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_margin="16dp"
    android:src="@drawable/next" />

or with a fragment tag

<fragment 
android:name="com.samplefrag"
android:id="@+id/samplefrag"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />

in fragment container you can add fragments dynamically with fragment transaction too ,like see below

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            HelloFragment hello = new HelloFragment();
            fragmentTransaction.add(R.id.frame_container, hello, "HELLO");
            fragmentTransaction.commit();

UPDATE

for your problem, You want to display main activity when you press home button,so you can use Intent and send data with intent,then recieve that data into Main Activity and use fragment transaction according to it to display the fragment in your activity

Upvotes: 0

Suraj Nair
Suraj Nair

Reputation: 1847

As @Noise Generator commented you just have to replace your fragment with Home fragment like this :-

If you only want to use the fragment as the top level content view of the activity, then

    Fragment newFragment = new HomeFragment();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(android.R.id.content, newFragment).commit();

other wise as @Dheeraj Joshi stated you have to create a resource and then replace it

    Fragment newFragment = new HomeFragment();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.frame_container, newFragment).commit();

Upvotes: 1

Related Questions