Send Object from One Fragment to Another
I have two fragments in which1st fragment(Business) there is a object stringlist that needs to be transferred to 2nd fragment(Businessdetail).
I want to know what is the best method of practise and how should i do it?
public class Business extends Fragment {
public List<StringList> businessNews = new ArrayList<>();
private RecyclerView recyclerView;
StringList stringList; //object need to transfered to other fragment
public Business() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.business_recycler_view);
FetchLists f = new FetchLists();
f.execute(10, 0);
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public class FetchLists extends AsyncTask<Integer, Void, List<StringList>> {
@Override
protected List<StringList> doInBackground(Integer... params) {
int count = params[0];
int offset = params[1];
String urlString = "https://nei.org/v1/articlesbjkbknklnmlmerg&sortBy=top&apiKey=50e2bjkbbkba5a5f476ff528a8";
urlString = urlString + "&count=" + count + "&offset=" + offset;
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line = reader.readLine();
String response = "";
while (line != null) {
response += line;
line = reader.readLine();
}
JSONObject object = new JSONObject(response);
JSONArray emailLists = object.getJSONArray("articles");
for (int i = 0; i < emailLists.length(); i++) {
JSONObject listData = (JSONObject) emailLists.get(i);
stringList = new StringList();
stringList.authorName = listData.getString("author");
stringList.headline = listData.getString("title");
stringList.publishedTime = listData.getString("publishedAt");
stringList.newsDetail = listData.getString("description");
businessNews.add(stringList);
Log.d("ashu", "authorname" + stringList.authorName);
}
} catch (Exception e) {
e.printStackTrace();
}
return businessNews;
}
public class BusinessAdapter extends RecyclerView.Adapter<BusinessHolder> {
@Override
public BusinessHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_news, parent, false);
return new BusinessHolder(view);
}
@Override
public void onBindViewHolder(BusinessHolder holder, int position) {
StringList m = c.get(position);
holder.bindListName(m, position);
}}
public class BusinessHolder extends RecyclerView.ViewHolder {
public TextView headlineTextview;
public TextView authorTextview;
public TextView timeTextview;
public BusinessHolder(View itemView) {
super(itemView);
headlineTextview = (TextView) itemView.findViewById(R.id.id_headline);
authorTextview = (TextView) itemView.findViewById(R.id.id_author);
timeTextview = (TextView) itemView.findViewById(R.id.id_time);
}
2nd Fragment:
In this fragment i want to set the object data to the Textview Parameters
public class BusinessDetail extends Fragment {
StringList mstringList;
private TextView headlineSecond;
public TextView authorSecond;
private TextView detailsSecond;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business_detail, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
headlineSecond = (TextView) view.findViewById(R.id.id_headline_second);
authorSecond = (TextView) view.findViewById(R.id.id_author_second);
detailsSecond = (TextView) view.findViewById(R.id.id_details_second);
}}
android object android-fragments android-fragmentactivity
add a comment |
I have two fragments in which1st fragment(Business) there is a object stringlist that needs to be transferred to 2nd fragment(Businessdetail).
I want to know what is the best method of practise and how should i do it?
public class Business extends Fragment {
public List<StringList> businessNews = new ArrayList<>();
private RecyclerView recyclerView;
StringList stringList; //object need to transfered to other fragment
public Business() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.business_recycler_view);
FetchLists f = new FetchLists();
f.execute(10, 0);
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public class FetchLists extends AsyncTask<Integer, Void, List<StringList>> {
@Override
protected List<StringList> doInBackground(Integer... params) {
int count = params[0];
int offset = params[1];
String urlString = "https://nei.org/v1/articlesbjkbknklnmlmerg&sortBy=top&apiKey=50e2bjkbbkba5a5f476ff528a8";
urlString = urlString + "&count=" + count + "&offset=" + offset;
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line = reader.readLine();
String response = "";
while (line != null) {
response += line;
line = reader.readLine();
}
JSONObject object = new JSONObject(response);
JSONArray emailLists = object.getJSONArray("articles");
for (int i = 0; i < emailLists.length(); i++) {
JSONObject listData = (JSONObject) emailLists.get(i);
stringList = new StringList();
stringList.authorName = listData.getString("author");
stringList.headline = listData.getString("title");
stringList.publishedTime = listData.getString("publishedAt");
stringList.newsDetail = listData.getString("description");
businessNews.add(stringList);
Log.d("ashu", "authorname" + stringList.authorName);
}
} catch (Exception e) {
e.printStackTrace();
}
return businessNews;
}
public class BusinessAdapter extends RecyclerView.Adapter<BusinessHolder> {
@Override
public BusinessHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_news, parent, false);
return new BusinessHolder(view);
}
@Override
public void onBindViewHolder(BusinessHolder holder, int position) {
StringList m = c.get(position);
holder.bindListName(m, position);
}}
public class BusinessHolder extends RecyclerView.ViewHolder {
public TextView headlineTextview;
public TextView authorTextview;
public TextView timeTextview;
public BusinessHolder(View itemView) {
super(itemView);
headlineTextview = (TextView) itemView.findViewById(R.id.id_headline);
authorTextview = (TextView) itemView.findViewById(R.id.id_author);
timeTextview = (TextView) itemView.findViewById(R.id.id_time);
}
2nd Fragment:
In this fragment i want to set the object data to the Textview Parameters
public class BusinessDetail extends Fragment {
StringList mstringList;
private TextView headlineSecond;
public TextView authorSecond;
private TextView detailsSecond;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business_detail, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
headlineSecond = (TextView) view.findViewById(R.id.id_headline_second);
authorSecond = (TextView) view.findViewById(R.id.id_author_second);
detailsSecond = (TextView) view.findViewById(R.id.id_details_second);
}}
android object android-fragments android-fragmentactivity
which object you want to transfer from one fragment to another in your code
– Sandeep dhiman
May 29 '17 at 17:14
Are both Fragments in the same activity?
– Juan
May 29 '17 at 17:15
yes both fragment are in same activity
– user7620502
May 29 '17 at 17:16
"stringlist" is the object that i want to tranfer
– user7620502
May 29 '17 at 17:17
add a comment |
I have two fragments in which1st fragment(Business) there is a object stringlist that needs to be transferred to 2nd fragment(Businessdetail).
I want to know what is the best method of practise and how should i do it?
public class Business extends Fragment {
public List<StringList> businessNews = new ArrayList<>();
private RecyclerView recyclerView;
StringList stringList; //object need to transfered to other fragment
public Business() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.business_recycler_view);
FetchLists f = new FetchLists();
f.execute(10, 0);
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public class FetchLists extends AsyncTask<Integer, Void, List<StringList>> {
@Override
protected List<StringList> doInBackground(Integer... params) {
int count = params[0];
int offset = params[1];
String urlString = "https://nei.org/v1/articlesbjkbknklnmlmerg&sortBy=top&apiKey=50e2bjkbbkba5a5f476ff528a8";
urlString = urlString + "&count=" + count + "&offset=" + offset;
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line = reader.readLine();
String response = "";
while (line != null) {
response += line;
line = reader.readLine();
}
JSONObject object = new JSONObject(response);
JSONArray emailLists = object.getJSONArray("articles");
for (int i = 0; i < emailLists.length(); i++) {
JSONObject listData = (JSONObject) emailLists.get(i);
stringList = new StringList();
stringList.authorName = listData.getString("author");
stringList.headline = listData.getString("title");
stringList.publishedTime = listData.getString("publishedAt");
stringList.newsDetail = listData.getString("description");
businessNews.add(stringList);
Log.d("ashu", "authorname" + stringList.authorName);
}
} catch (Exception e) {
e.printStackTrace();
}
return businessNews;
}
public class BusinessAdapter extends RecyclerView.Adapter<BusinessHolder> {
@Override
public BusinessHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_news, parent, false);
return new BusinessHolder(view);
}
@Override
public void onBindViewHolder(BusinessHolder holder, int position) {
StringList m = c.get(position);
holder.bindListName(m, position);
}}
public class BusinessHolder extends RecyclerView.ViewHolder {
public TextView headlineTextview;
public TextView authorTextview;
public TextView timeTextview;
public BusinessHolder(View itemView) {
super(itemView);
headlineTextview = (TextView) itemView.findViewById(R.id.id_headline);
authorTextview = (TextView) itemView.findViewById(R.id.id_author);
timeTextview = (TextView) itemView.findViewById(R.id.id_time);
}
2nd Fragment:
In this fragment i want to set the object data to the Textview Parameters
public class BusinessDetail extends Fragment {
StringList mstringList;
private TextView headlineSecond;
public TextView authorSecond;
private TextView detailsSecond;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business_detail, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
headlineSecond = (TextView) view.findViewById(R.id.id_headline_second);
authorSecond = (TextView) view.findViewById(R.id.id_author_second);
detailsSecond = (TextView) view.findViewById(R.id.id_details_second);
}}
android object android-fragments android-fragmentactivity
I have two fragments in which1st fragment(Business) there is a object stringlist that needs to be transferred to 2nd fragment(Businessdetail).
I want to know what is the best method of practise and how should i do it?
public class Business extends Fragment {
public List<StringList> businessNews = new ArrayList<>();
private RecyclerView recyclerView;
StringList stringList; //object need to transfered to other fragment
public Business() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.business_recycler_view);
FetchLists f = new FetchLists();
f.execute(10, 0);
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public class FetchLists extends AsyncTask<Integer, Void, List<StringList>> {
@Override
protected List<StringList> doInBackground(Integer... params) {
int count = params[0];
int offset = params[1];
String urlString = "https://nei.org/v1/articlesbjkbknklnmlmerg&sortBy=top&apiKey=50e2bjkbbkba5a5f476ff528a8";
urlString = urlString + "&count=" + count + "&offset=" + offset;
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line = reader.readLine();
String response = "";
while (line != null) {
response += line;
line = reader.readLine();
}
JSONObject object = new JSONObject(response);
JSONArray emailLists = object.getJSONArray("articles");
for (int i = 0; i < emailLists.length(); i++) {
JSONObject listData = (JSONObject) emailLists.get(i);
stringList = new StringList();
stringList.authorName = listData.getString("author");
stringList.headline = listData.getString("title");
stringList.publishedTime = listData.getString("publishedAt");
stringList.newsDetail = listData.getString("description");
businessNews.add(stringList);
Log.d("ashu", "authorname" + stringList.authorName);
}
} catch (Exception e) {
e.printStackTrace();
}
return businessNews;
}
public class BusinessAdapter extends RecyclerView.Adapter<BusinessHolder> {
@Override
public BusinessHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_news, parent, false);
return new BusinessHolder(view);
}
@Override
public void onBindViewHolder(BusinessHolder holder, int position) {
StringList m = c.get(position);
holder.bindListName(m, position);
}}
public class BusinessHolder extends RecyclerView.ViewHolder {
public TextView headlineTextview;
public TextView authorTextview;
public TextView timeTextview;
public BusinessHolder(View itemView) {
super(itemView);
headlineTextview = (TextView) itemView.findViewById(R.id.id_headline);
authorTextview = (TextView) itemView.findViewById(R.id.id_author);
timeTextview = (TextView) itemView.findViewById(R.id.id_time);
}
2nd Fragment:
In this fragment i want to set the object data to the Textview Parameters
public class BusinessDetail extends Fragment {
StringList mstringList;
private TextView headlineSecond;
public TextView authorSecond;
private TextView detailsSecond;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business_detail, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
headlineSecond = (TextView) view.findViewById(R.id.id_headline_second);
authorSecond = (TextView) view.findViewById(R.id.id_author_second);
detailsSecond = (TextView) view.findViewById(R.id.id_details_second);
}}
android object android-fragments android-fragmentactivity
android object android-fragments android-fragmentactivity
edited May 29 '17 at 18:12
Quick learner
2,5451924
2,5451924
asked May 29 '17 at 17:07
user7620502
which object you want to transfer from one fragment to another in your code
– Sandeep dhiman
May 29 '17 at 17:14
Are both Fragments in the same activity?
– Juan
May 29 '17 at 17:15
yes both fragment are in same activity
– user7620502
May 29 '17 at 17:16
"stringlist" is the object that i want to tranfer
– user7620502
May 29 '17 at 17:17
add a comment |
which object you want to transfer from one fragment to another in your code
– Sandeep dhiman
May 29 '17 at 17:14
Are both Fragments in the same activity?
– Juan
May 29 '17 at 17:15
yes both fragment are in same activity
– user7620502
May 29 '17 at 17:16
"stringlist" is the object that i want to tranfer
– user7620502
May 29 '17 at 17:17
which object you want to transfer from one fragment to another in your code
– Sandeep dhiman
May 29 '17 at 17:14
which object you want to transfer from one fragment to another in your code
– Sandeep dhiman
May 29 '17 at 17:14
Are both Fragments in the same activity?
– Juan
May 29 '17 at 17:15
Are both Fragments in the same activity?
– Juan
May 29 '17 at 17:15
yes both fragment are in same activity
– user7620502
May 29 '17 at 17:16
yes both fragment are in same activity
– user7620502
May 29 '17 at 17:16
"stringlist" is the object that i want to tranfer
– user7620502
May 29 '17 at 17:17
"stringlist" is the object that i want to tranfer
– user7620502
May 29 '17 at 17:17
add a comment |
5 Answers
5
active
oldest
votes
Fragments are not supposed to know about each other.
Instead of tranfering from one fragment to another, declare the object list in the activity and have each fragment get it from there:
To do that in main activity declare the object list and a getter for the list:
public List<StringList> businessNews = new ArrayList<>();
public List<StringList> getObjectList(){
return objectList;
}
Then in the Fragments, you can get the list:
((MainActivity) getActivity()).getObjectList();
You could make this call in onResume() to make sure the Fragment and the Activity are ready.
For a more correct solution, this part ((MainActivity) getActivity())
could be implemented using an interface so as to avoid the casting.
Thanx it worked!
– user7620502
May 30 '17 at 2:58
May i know , how to make a loader to make it running till my mews are fetched from the api?
– user7620502
May 30 '17 at 2:59
You can use a indeterminate progress bar o an existing library like crystal-preloaders: github.com/syedowaisali/crystal-preloaders.
– Juan
May 30 '17 at 13:55
add a comment |
If BusinessDetail and Business are children of the same activity, you should provide an interface between both fragments and the activity. In your Business fragment, you could make this call (in the fragment's onAttach or after):
((MyActivity)getActivity()).showObjectOnBusiness(stringList);
In the MyActivity showObjectOnBusiness method, you should pass the object to the BusinessDetail fragment:
Bundle bundle = new Bundle();
bundle.putParcelable(BusinessDetail.OBJECT_KEY, stringList);
new BusinessDetail().setArguments(bundle);
Inside your BusinessDetail, you may obtain your object through the arguments:
Bundle bundle = getArguments();
if (bundle == null || !bundle.containsKey(OBJECT_KEY)) {
throw new IllegalArgumentException("StringList should not be null");
}
StringList stringList = bundle.getParcelable(OBJECT_KEY);
StringList should implement Parcelable.
add a comment |
Saving reference to fragment in another one - bad practice. You can use this method, it works fine.
add a comment |
You can achieve this through following code
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
You can also use Serializable but serializable is slower than parcelable
Bundle bundle = new Bundle();
bundles.putSerializable("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
add a comment |
You can use
Event Bus library
It will simplify the communication between components,
works on publisher/subscriber pattern.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44247482%2fsend-object-from-one-fragment-to-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Fragments are not supposed to know about each other.
Instead of tranfering from one fragment to another, declare the object list in the activity and have each fragment get it from there:
To do that in main activity declare the object list and a getter for the list:
public List<StringList> businessNews = new ArrayList<>();
public List<StringList> getObjectList(){
return objectList;
}
Then in the Fragments, you can get the list:
((MainActivity) getActivity()).getObjectList();
You could make this call in onResume() to make sure the Fragment and the Activity are ready.
For a more correct solution, this part ((MainActivity) getActivity())
could be implemented using an interface so as to avoid the casting.
Thanx it worked!
– user7620502
May 30 '17 at 2:58
May i know , how to make a loader to make it running till my mews are fetched from the api?
– user7620502
May 30 '17 at 2:59
You can use a indeterminate progress bar o an existing library like crystal-preloaders: github.com/syedowaisali/crystal-preloaders.
– Juan
May 30 '17 at 13:55
add a comment |
Fragments are not supposed to know about each other.
Instead of tranfering from one fragment to another, declare the object list in the activity and have each fragment get it from there:
To do that in main activity declare the object list and a getter for the list:
public List<StringList> businessNews = new ArrayList<>();
public List<StringList> getObjectList(){
return objectList;
}
Then in the Fragments, you can get the list:
((MainActivity) getActivity()).getObjectList();
You could make this call in onResume() to make sure the Fragment and the Activity are ready.
For a more correct solution, this part ((MainActivity) getActivity())
could be implemented using an interface so as to avoid the casting.
Thanx it worked!
– user7620502
May 30 '17 at 2:58
May i know , how to make a loader to make it running till my mews are fetched from the api?
– user7620502
May 30 '17 at 2:59
You can use a indeterminate progress bar o an existing library like crystal-preloaders: github.com/syedowaisali/crystal-preloaders.
– Juan
May 30 '17 at 13:55
add a comment |
Fragments are not supposed to know about each other.
Instead of tranfering from one fragment to another, declare the object list in the activity and have each fragment get it from there:
To do that in main activity declare the object list and a getter for the list:
public List<StringList> businessNews = new ArrayList<>();
public List<StringList> getObjectList(){
return objectList;
}
Then in the Fragments, you can get the list:
((MainActivity) getActivity()).getObjectList();
You could make this call in onResume() to make sure the Fragment and the Activity are ready.
For a more correct solution, this part ((MainActivity) getActivity())
could be implemented using an interface so as to avoid the casting.
Fragments are not supposed to know about each other.
Instead of tranfering from one fragment to another, declare the object list in the activity and have each fragment get it from there:
To do that in main activity declare the object list and a getter for the list:
public List<StringList> businessNews = new ArrayList<>();
public List<StringList> getObjectList(){
return objectList;
}
Then in the Fragments, you can get the list:
((MainActivity) getActivity()).getObjectList();
You could make this call in onResume() to make sure the Fragment and the Activity are ready.
For a more correct solution, this part ((MainActivity) getActivity())
could be implemented using an interface so as to avoid the casting.
answered May 29 '17 at 17:28
JuanJuan
4,5012919
4,5012919
Thanx it worked!
– user7620502
May 30 '17 at 2:58
May i know , how to make a loader to make it running till my mews are fetched from the api?
– user7620502
May 30 '17 at 2:59
You can use a indeterminate progress bar o an existing library like crystal-preloaders: github.com/syedowaisali/crystal-preloaders.
– Juan
May 30 '17 at 13:55
add a comment |
Thanx it worked!
– user7620502
May 30 '17 at 2:58
May i know , how to make a loader to make it running till my mews are fetched from the api?
– user7620502
May 30 '17 at 2:59
You can use a indeterminate progress bar o an existing library like crystal-preloaders: github.com/syedowaisali/crystal-preloaders.
– Juan
May 30 '17 at 13:55
Thanx it worked!
– user7620502
May 30 '17 at 2:58
Thanx it worked!
– user7620502
May 30 '17 at 2:58
May i know , how to make a loader to make it running till my mews are fetched from the api?
– user7620502
May 30 '17 at 2:59
May i know , how to make a loader to make it running till my mews are fetched from the api?
– user7620502
May 30 '17 at 2:59
You can use a indeterminate progress bar o an existing library like crystal-preloaders: github.com/syedowaisali/crystal-preloaders.
– Juan
May 30 '17 at 13:55
You can use a indeterminate progress bar o an existing library like crystal-preloaders: github.com/syedowaisali/crystal-preloaders.
– Juan
May 30 '17 at 13:55
add a comment |
If BusinessDetail and Business are children of the same activity, you should provide an interface between both fragments and the activity. In your Business fragment, you could make this call (in the fragment's onAttach or after):
((MyActivity)getActivity()).showObjectOnBusiness(stringList);
In the MyActivity showObjectOnBusiness method, you should pass the object to the BusinessDetail fragment:
Bundle bundle = new Bundle();
bundle.putParcelable(BusinessDetail.OBJECT_KEY, stringList);
new BusinessDetail().setArguments(bundle);
Inside your BusinessDetail, you may obtain your object through the arguments:
Bundle bundle = getArguments();
if (bundle == null || !bundle.containsKey(OBJECT_KEY)) {
throw new IllegalArgumentException("StringList should not be null");
}
StringList stringList = bundle.getParcelable(OBJECT_KEY);
StringList should implement Parcelable.
add a comment |
If BusinessDetail and Business are children of the same activity, you should provide an interface between both fragments and the activity. In your Business fragment, you could make this call (in the fragment's onAttach or after):
((MyActivity)getActivity()).showObjectOnBusiness(stringList);
In the MyActivity showObjectOnBusiness method, you should pass the object to the BusinessDetail fragment:
Bundle bundle = new Bundle();
bundle.putParcelable(BusinessDetail.OBJECT_KEY, stringList);
new BusinessDetail().setArguments(bundle);
Inside your BusinessDetail, you may obtain your object through the arguments:
Bundle bundle = getArguments();
if (bundle == null || !bundle.containsKey(OBJECT_KEY)) {
throw new IllegalArgumentException("StringList should not be null");
}
StringList stringList = bundle.getParcelable(OBJECT_KEY);
StringList should implement Parcelable.
add a comment |
If BusinessDetail and Business are children of the same activity, you should provide an interface between both fragments and the activity. In your Business fragment, you could make this call (in the fragment's onAttach or after):
((MyActivity)getActivity()).showObjectOnBusiness(stringList);
In the MyActivity showObjectOnBusiness method, you should pass the object to the BusinessDetail fragment:
Bundle bundle = new Bundle();
bundle.putParcelable(BusinessDetail.OBJECT_KEY, stringList);
new BusinessDetail().setArguments(bundle);
Inside your BusinessDetail, you may obtain your object through the arguments:
Bundle bundle = getArguments();
if (bundle == null || !bundle.containsKey(OBJECT_KEY)) {
throw new IllegalArgumentException("StringList should not be null");
}
StringList stringList = bundle.getParcelable(OBJECT_KEY);
StringList should implement Parcelable.
If BusinessDetail and Business are children of the same activity, you should provide an interface between both fragments and the activity. In your Business fragment, you could make this call (in the fragment's onAttach or after):
((MyActivity)getActivity()).showObjectOnBusiness(stringList);
In the MyActivity showObjectOnBusiness method, you should pass the object to the BusinessDetail fragment:
Bundle bundle = new Bundle();
bundle.putParcelable(BusinessDetail.OBJECT_KEY, stringList);
new BusinessDetail().setArguments(bundle);
Inside your BusinessDetail, you may obtain your object through the arguments:
Bundle bundle = getArguments();
if (bundle == null || !bundle.containsKey(OBJECT_KEY)) {
throw new IllegalArgumentException("StringList should not be null");
}
StringList stringList = bundle.getParcelable(OBJECT_KEY);
StringList should implement Parcelable.
edited May 29 '17 at 17:37
answered May 29 '17 at 17:23
jscjsc
1037
1037
add a comment |
add a comment |
Saving reference to fragment in another one - bad practice. You can use this method, it works fine.
add a comment |
Saving reference to fragment in another one - bad practice. You can use this method, it works fine.
add a comment |
Saving reference to fragment in another one - bad practice. You can use this method, it works fine.
Saving reference to fragment in another one - bad practice. You can use this method, it works fine.
answered May 29 '17 at 17:25
Ovechkin PavelOvechkin Pavel
659522
659522
add a comment |
add a comment |
You can achieve this through following code
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
You can also use Serializable but serializable is slower than parcelable
Bundle bundle = new Bundle();
bundles.putSerializable("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
add a comment |
You can achieve this through following code
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
You can also use Serializable but serializable is slower than parcelable
Bundle bundle = new Bundle();
bundles.putSerializable("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
add a comment |
You can achieve this through following code
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
You can also use Serializable but serializable is slower than parcelable
Bundle bundle = new Bundle();
bundles.putSerializable("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
You can achieve this through following code
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
You can also use Serializable but serializable is slower than parcelable
Bundle bundle = new Bundle();
bundles.putSerializable("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
answered May 29 '17 at 17:29
Sandeep dhimanSandeep dhiman
7341615
7341615
add a comment |
add a comment |
You can use
Event Bus library
It will simplify the communication between components,
works on publisher/subscriber pattern.
add a comment |
You can use
Event Bus library
It will simplify the communication between components,
works on publisher/subscriber pattern.
add a comment |
You can use
Event Bus library
It will simplify the communication between components,
works on publisher/subscriber pattern.
You can use
Event Bus library
It will simplify the communication between components,
works on publisher/subscriber pattern.
answered May 29 '17 at 17:34
Rachit SolankiRachit Solanki
334411
334411
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44247482%2fsend-object-from-one-fragment-to-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
which object you want to transfer from one fragment to another in your code
– Sandeep dhiman
May 29 '17 at 17:14
Are both Fragments in the same activity?
– Juan
May 29 '17 at 17:15
yes both fragment are in same activity
– user7620502
May 29 '17 at 17:16
"stringlist" is the object that i want to tranfer
– user7620502
May 29 '17 at 17:17