[英]send data from IntentService to Fragment
當我在這個問題中嘗試使用BroadCastReciever時,我嘗試在我的日志中使用參數傳遞數據也沒關系。
Bundle bundle = new Bundle();
ArticleDetailFragment fragobj = new ArticleDetailFragment().newInstance2(jsonString);
fragobj.setArguments(bundle);
Log.i(TAG, "onHandleIntent: " + bundle);
但我收到了null:
public static ArticleDetailFragment newInstance2(String item) {
Bundle arguments = new Bundle();
arguments.putString("edttext", item);
ArticleDetailFragment fragment = new ArticleDetailFragment();
fragment.setArguments(arguments);
return fragment;
}
在我的onCreateView()中:
Log.i(TAG, "onCreateViewBundle: " + getArguments().getString("edttext"));
最好的方法是什么?
這是因為您首先使用正確的數據設置正確的片段參數,然后在newInstance2之外覆蓋它。
Bundle bundle = new Bundle();
ArticleDetailFragment fragobj = new ArticleDetailFragment().newInstance2(jsonString);
fragobj.setArguments(bundle); // <- here you override the arguments that were set inside newInstance with a bundle that has no data in it
Log.i(TAG, "onHandleIntent: " + bundle);
應該
ArticleDetailFragment fragobj = ArticleDetailFragment().newInstance2(jsonString);
Log.i(TAG, "onHandleIntent: " + fragobj.getArguments().getString("edttext"));
另一個答案是刪除new
關鍵字。 他是對的!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.