Hello fellow programmers!
I am new to android and I am developing an application. I am stuck at a point where i need to pass data contained in an object from activity A to activity B. I am pasting my code below please review it and let me know if I am doing it right or wrong and suggest best practice for doing it ... Should I be passing data in an object or should I do something else? ... please suggest.
Code for Activity A
public MainListActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_list, container, false);
listView = (ListView) rootView.findViewById(R.id.listView_recipes);
mProgressBar = (ProgressBar) rootView.findViewById(R.id.recipe_listview_progressBar);
if(isNetworkAvailable()) {
mProgressBar.setVisibility(View.VISIBLE);
GetRecipeData getRecipeData = new GetRecipeData();
getRecipeData.execute();
} else {
Toast.makeText(getContext(),getString(R.string.no_network), Toast.LENGTH_LONG).show();
}
//mRecipeTitles = new ArrayList<>();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object recipeData = mRecipeAdapter.getItem(position);
Intent intent = new Intent(getActivity(), RecipeDetailActivity.class).putExtra("data", (Serializable) recipeData);
startActivity(intent);
}
});
return rootView;
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()){
isAvailable = true;
}
return isAvailable;
}
private void handleRecipeData() {
mProgressBar.setVisibility(View.INVISIBLE);
if(mRecipeData == null){
handleErrors();
}else {
try {
JSONArray jsonPosts = mRecipeData.getJSONArray("posts");
//mRecipePostData = new String[jsonPosts.length()];
mRecipePostData = new ArrayList<>();
for (int i = 0; i < jsonPosts.length(); i++){
JSONObject post = jsonPosts.getJSONObject(i);
String title = post.getString(KEY_TITLE);
title = Html.fromHtml(title).toString();
String author = post.getJSONObject(KEY_AUTHOR).getString("name");
author = Html.fromHtml(author).toString();
HashMap<String, String> singleRecipePost = new HashMap<>();
singleRecipePost.put(KEY_TITLE, title);
singleRecipePost.put(KEY_AUTHOR, author);
mRecipePostData.add(singleRecipePost);
//mRecipePostData[i] = title;
}
String[] keys = {KEY_TITLE, KEY_AUTHOR};
int[] ids = {android.R.id.text1, android.R.id.text2};
mRecipeAdapter = new SimpleAdapter(getContext(), mRecipePostData, android.R.layout.simple_list_item_2, keys, ids);
listView.setAdapter(mRecipeAdapter);
mRecipeAdapter.notifyDataSetChanged();
} catch (JSONException e) {
Log.e(LOG_TAG,"Exception Caught: ",e);
}
}
}
Code for Activity B
public class RecipeDetailActivityFragment extends Fragment {
public static final String LOG_TAG = RecipeDetailActivity.class.getSimpleName();
public static final String RECIPE_SHARE_HASHTAG = "#AmericanCuisines";
private String mRecipeDataStr;
public RecipeDetailActivityFragment() {
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent intent = getActivity().getIntent();
View rootView = inflater.inflate(R.layout.fragment_recipe_detail, container, false);
if (intent != null && intent.hasExtra("data")){
mRecipeDataStr = intent.getSerializableExtra("data");
TextView details = (TextView) rootView.findViewById(R.id.recipe_detail_textView);
details.setText(mRecipeDataStr);
}
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.recipedetailfragment, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(createShareRecipeIntent());
} else {
Log.d(LOG_TAG, "Share Action Provider is null?");
}
}
public Intent createShareRecipeIntent(){
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mRecipeDataStr + " " + RECIPE_SHARE_HASHTAG);
return shareIntent;
}
}
I don't think so that my question is a duplicate of the referenced question because I am trying to pass serialized data an array through activities and an I am having issue in doing so ... my question is that am I doing this right ... should I be passing data contained in an object through activities ? because I will be passing alot more .. like the whole content ... images ... etc.