简体   繁体   中英

How can I get access to a fragment method from another activity

I have a fragment which summons a custom CursorAdaper and display it on a ListView . The thing is I want to change the cursor by changeCursor() from another activity when I add new data, How can I get access to the CursorAdapter displayed on the fragment?

Essentially you have to pass data from one Activity to another and let the fragment of you choice receive the data (each fragment of a given Activity may get the Intent that started/restarted/resumed the Activity).

Consider this code

-- pass data:

String[] myListEntries = getNewListContents();
Intent updateList = new Intent(this, ActivityThatListFragmentBelongsTo.class);
updateList.putExtra("updated_list", myListEntries);
startActivity(updateList);

-- receive data (in fragment):

@Override
public void onResume() {
  Intent wasStartedWithData = getActivity().getIntent();
  String[] updatedList = wasStartedWithData.getStringArrayExtra("updated_list");
  // pass updatedList to adapter
}

Now you might actually have more complicated data than an Array of Strings. In this case you could create a class that implements 'Parcelable' ( http://developer.android.com/reference/android/os/Parcelable.html ) and call putExtra(Parcelable parcel) / getParcelableExtra(String name) on the Intent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM