我有一个类似于以下内容的片段,我知道如何通过制作参数并将它们附加到Bundle参数来在片段之间进行通信。在这种情况下,该片段将调用一个新片段,并将覆盖旧的显示片段。然后我需要在片段到新片段之间传递值。当我按下Actionbar
按钮上的共享菜单时,我希望url共享与当前共享匹配,如果我使用log对其进行管理,但仍然是旧值,则可以正常工作
public class ReadingFragment extends Fragment implements
ReadFragView,
RelatedNewsAdapter.RelatedNewsCallback {
private static final String ARG_GUID = "ARG_GUID";
public static final String ARG_BOOKMARK = "ARG_BOOKMARK";
public static final String ARG_URLSHARE = "ARG_URLSHARE";
private Unbinder unbinder;
private String guidData;
private OnFragmentInteractionListener mListener;
private ReadFragPresenter readFragPresenter;
private FragmentManager fragmentManager;
private String urlForSharing;
private Bookmark bookmark;
public ReadingFragment() {
// Required empty public constructor
}
public static ReadingFragment newInstance(String guid, Bookmark bookmark, String urlShare) {
ReadingFragment fragment = new ReadingFragment();
Bundle args = new Bundle();
args.putString(ARG_GUID, guid);
if(bookmark != null){
args.putSerializable(ARG_BOOKMARK, bookmark);
}
if(urlShare != null){
// --- THIS LINE IS WORKING --- //
args.putString(ARG_URLSHARE, urlShare);
}
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
readFragPresenter = new ReadFragPresenterImpl(this, getActivity().getApplication());
if (getArguments() != null) {
guidData = getArguments().getString(ARG_GUID);
bookmark = (Bookmark) getArguments().getSerializable(ARG_BOOKMARK);
// --- THIS LINE IS WORKING TOO, VALUE URLFORSHARING IS CORRECT ---
urlForSharing = getArguments().getString(ARG_URLSHARE);
Timber.d("OnCreate urlShare %s", urlForSharing);
}
fragmentManager = getFragmentManager();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setRetainInstance(true);
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_reading, container, false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_reading, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.share:
// ---THIS IS THE PROBLEM, THIS LINE IS STILL OLD VALUE --- //
Timber.d("urlSharing selected %s", urlForSharing);
// --- THIS IS THE PROBLEM -----//
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, ""+ DeviceUtil.getApplicationName(getContext()));
shareIntent.putExtra(Intent.EXTRA_TEXT, urlForSharing);
startActivity(Intent.createChooser(shareIntent, "Share Via"));
return true;
case R.id.bookmark:
Timber.d("select item %s", bookmark.toString());
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void resultLoadSelectedNews(boolean isSuccess, MotherArticle motherArticle) {
if(isSuccess){
Article article = motherArticle.getArticles().get(0);
//RELATED NEWS
RelatedNewsAdapter relatedNewsAdapter = new RelatedNewsAdapter(getContext(),
motherArticle.getRelates(), this);
recycleRelatedNews.setLayoutManager(new LinearLayoutManager(getContext()));
recycleRelatedNews.setAdapter(relatedNewsAdapter);
// FOR SHARING (FIRST OPENING)
urlForSharing = article.getMoreLink();
// FOR BOOKMARK (FIRST OPENING)
bookmark = new Bookmark();
bookmark.setTitle(article.getHeadLine());
Timber.d("isi bookmark -> %s", bookmark.toString());
}
progressBar.setVisibility(View.GONE);
}
@Override
public void itemClick(Related related) {
bookmark = new Bookmark();
bookmark.setTitle(related.getTitle());
// --- VALUE URLSHARE IS UPDATED HERE--//
String urlShare = related.getLink();
Timber.d("urlShared %s", urlShare);
ReadingFragment fragment = ReadingFragment.newInstance(related.getGuid(), bookmark, urlShare);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
transaction.addToBackStack(null);
transaction.add(R.id.dynamic_fragment_frame, fragment, "FRAGMENT_"+related.getGuid());
transaction.commit();
}
}
onOptionsItemSelected
上的url仍然是旧的url,即使它应该已经被新的url替换了,上面的url仍然是旧的url,即使它已经被新的url替换了。通过Bundle参数进行了更新。