android.support.v4.app.FragmentManager OR android.app.FragmentManager?
Asked Answered
B

4

8

I'm learning Android development. I get stuck at something that should be very easy.

Im creating an App with one Activity, 2 fragments and 1 interface.

android:minSdkVersion="11"
android:targetSdkVersion="19

So in the main activity Im trying to create a reference to Fragment B using the manager. I get stuck here, because Eclispse is telling me to change some things (see below):

My intension:`

@Override
    public void respond(int i) {
        // TODO Auto-generated method stub

    FragmentManager manager =getFragmentManager();
    FragmentB f2= (FragmentB) manager.findFragmentById(R.id.fragment2);

}`

If I do it this whay I get the error messages and need to perform some changes. After the changes the code looks like this (and I still can't reach FragmentB):

    @Override
public void respond(int i) {
    // TODO Auto-generated method stub

    android.app.FragmentManager manager =getFragmentManager();
    android.app.Fragment f2=  manager.findFragmentById(R.id.fragment2);

}

For extra details I'll put here also the import header of the Activity:

  package com.example.modular_ui;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends Activity implements Communicator{....

What am I missing here? the whole support.v4 /support.v7 thing is a little confusing for rookies.

EDIT: After changing to:

    import android.app.Fragment;
import android.app.FragmentManager;

AND extending FragmentActivity I still can't create a reference to FragmentB:

@Override
public void respond(int i) {
    // TODO Auto-generated method stub

FragmentManager man = getFragmentManager();
FragmentB b = man.findFragmentById(R.id.fragment2);

}

As Requested I've posted the FragmentB code:

package com.example.modular_ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentB extends Fragment {

TextView text;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.fragment_b, container);
}

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        text = (TextView) getActivity().findViewById(R.id.textView1);
    }

Main XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.modular_ui.MainActivity"
    tools:ignore="MergeRootFrame" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.modular_ui.FragmentA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.modular_ui.FragmentB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/fragment1"
        android:layout_marginTop="54dp" />

</RelativeLayout>
Bagger answered 13/4, 2014 at 15:54 Comment(3)
The support library is primarily to support the ActionBar and a few other key features that weren't supported until API 11. If you have a min API of 11 I would just use the regular library to avoid adding unneeded bulk. Unless there's something I'm unaware of.Cate
Thanks for your quick response. Eclipse automatically implements the support lib. So you suggest to change this to: import android.app.Fragment; import android.app.FragmentManager;Bagger
You can use either, but I don't think there is a need if your at an API higher at 11 or higher as everything (that I know of) is supported with the native libraries at that point. If that's the case it is just bulk in your app and can cause a headache trying to remember which you're using.Cate
R
4

First of all: your activity should extend FragmentActivity.

About support libraries. They were introduced to add some functionalities to older Androids. For example Fragments were introduced in Android 3.0 (SDK nr: 11). In fact (according to documentation) in Androids 3.0 < support libary uses system implementation of Fragments.

Roger answered 13/4, 2014 at 16:1 Comment(4)
Can you post your code for creating and attaching FragmentB? Also main activity xml would be helpful.Roger
I have added these things underneathBagger
Your fragment extends support library's fragment. Change import android.support.v4.app.Fragment; to import android.app.Fragment; in FragmentBRoger
Outdated answer. Because OP is targeting API 11 and newer, does not need the support v4 stuff, and therefore does not need FragmentActivity (which is part of support v4). As RobertM says, the real problem is that his Fragment did import android.support.v4.app.Fragment; Thereby committing him to this old approach. Rip out all references to support.v4. Just say import android.app.Fragment; Use a modern Fragment example from developer.android or elsewhere, that doesn't say support.v4 anywhere in it. Then you can simply say getFragmentManager().Stratocumulus
B
10

Simply use getSupportFragmentManager(); , after you added the support library successfully.

Baumgardner answered 5/9, 2014 at 13:14 Comment(0)
S
7

OP was so close to having a solution that would have worked fine for API 11 and newer without needing support.v4.

He just needed to change his Fragment to also not use support.v4, in its import statement.

Summary of the two approaches. ALL your Activities and Fragments should have code that looks like exactly one of these; do not mix them! (Not all lines are needed in all files; use lines as needed.)

support-v4 approach

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;      <-- ".support.v4"
import android.support.v4.app.FragmentManager;

... MainActivity extends FragmentActivity ...

... = getSupportFragmentManager();

.... YourFragment extends Fragment ... <-- same in both approaches

API 11+ approach

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;

... MainActivity extends Activity ...

... = getFragmentManager();

.... YourFragment extends Fragment ... <-- same in both approaches

So if you have a project that is written using one approach above, and you are integrating in code from elsewhere, be sure to look for these lines, and change them to match what you have.

Stratocumulus answered 21/9, 2015 at 14:31 Comment(0)
R
4

First of all: your activity should extend FragmentActivity.

About support libraries. They were introduced to add some functionalities to older Androids. For example Fragments were introduced in Android 3.0 (SDK nr: 11). In fact (according to documentation) in Androids 3.0 < support libary uses system implementation of Fragments.

Roger answered 13/4, 2014 at 16:1 Comment(4)
Can you post your code for creating and attaching FragmentB? Also main activity xml would be helpful.Roger
I have added these things underneathBagger
Your fragment extends support library's fragment. Change import android.support.v4.app.Fragment; to import android.app.Fragment; in FragmentBRoger
Outdated answer. Because OP is targeting API 11 and newer, does not need the support v4 stuff, and therefore does not need FragmentActivity (which is part of support v4). As RobertM says, the real problem is that his Fragment did import android.support.v4.app.Fragment; Thereby committing him to this old approach. Rip out all references to support.v4. Just say import android.app.Fragment; Use a modern Fragment example from developer.android or elsewhere, that doesn't say support.v4 anywhere in it. Then you can simply say getFragmentManager().Stratocumulus
R
2

It's simple.

If you also want your app to run in older devices (below API level 11), use getSupportFragmentManager().

If you want your app to run in devices with API Level above 11, then use getFragmentManger().

Reinwald answered 21/1, 2017 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.