Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.support.v4.app.FragmentManager OR android.app.FragmentManager?

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>
like image 624
Foo Avatar asked Apr 13 '14 15:04

Foo


4 Answers

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

like image 95
bgauryy Avatar answered Nov 16 '22 13:11

bgauryy


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.

like image 42
ToolmakerSteve Avatar answered Nov 16 '22 12:11

ToolmakerSteve


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.

like image 42
RobertM Avatar answered Nov 16 '22 12:11

RobertM


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().

like image 2
suresh yadam Avatar answered Nov 16 '22 13:11

suresh yadam