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>
Simply use getSupportFragmentManager(); , after you added the support library successfully.