Reputation: 3717
Overview of what I need:
Pass String from FragmentActivity
to FragmentList
. Said FragmentList
is specified in the xml as below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MultiContactsFragmentList" >
...
<fragment
android:id="@+id/multiContactsList_fragment"
android:name="com.sf.lidgit_android.content.MultiContactsFragmentList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
The problem in the code lies here (by the way, I'm using Support Library because I target API level 10), in my class that extends from FragmentActivity:
@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
...
inviteByEmail();
}
public void inviteByEmail(){
setContentView(R.layout.activity_multi_contacts_email_picker);
MultiContactsFragmentList fragmentList =
(MultiContactsFragmentList)
getSupportFragmentManager().findFragmentById(R.id.multiContactsList_fragment);
Bundle extras = new Bundle();
extras.putString(MultiContactsFragmentList.DATA_TO_PICK, MultiContactsFragmentList.EMAIL_DATA);
fragmentList.setArguments(extras);
}
Here is the thing, fragmentList is null
. I figure it's because of the life cycle (I'm not aware to the FragmetnActivity
-FragmentList
life cycle). The FragmentList is yet to be created. Is that it? I know the other way around would be to have my FragmentActivity "save" the data, and have the FragmentList access it (since FragmentActivity is available to it's Fragments, I believe).
EDIT
My FragmentList class (the fragment I'm trying to pass a Bundle to), implements LoaderManager.LoaderCallbacks<Cursor>
. I don't know if that influences or not, but I tried the first answer below and got an error on my FragmentList caused by a null pointer Exception, it appears that the onLoadFinished(..) method was called before the onCreate(..), because the null Pointer Exception
happened on the onLoadFinished(..), stating the adapter
was null.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("invite", "oncreate called");
String[] fromColumns = {ContactsContract.Contacts.DISPLAY_NAME, this.view_ID_Data};
int[] toViews = {R.id.contact_name, R.id.contact_data};
getLoaderManager().initLoader(MULTI_CONTACTS_LIST_LOADER, null, this);
this.adapter = new SimpleCursorAdapter(
getActivity().getApplicationContext(), R.layout.custom_list_contacts_multiple_choice,
null, fromColumns, toViews,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(adapter);
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
adapter.swapCursor(cursor);
}
Upvotes: 0
Views: 60
Reputation: 231
Change your layout to this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
And in your activity:
@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
...
inviteByEmail();
}
public void inviteByEmail(){
setContentView(R.layout.activity_multi_contacts_email_picker);
MultiContactsFragmentList fragmentList = new MultiContactsFragmentList();
Bundle extras = new Bundle();
extras.putString(MultiContactsFragmentList.DATA_TO_PICK, MultiContactsFragmentList.EMAIL_DATA);
fragmentList.setArguments(extras);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.content, fragmentList);
ft.commit();
}
The method setArguments(Bundle args) for the fragment can only be called just after creating the fragment, so if you embed you fragment in the XML the argumments setting may no be happening at all even if you call it. This way you have to attach the fragment yourself but you will have the arguments when you access it.
Upvotes: 1