Reputation: 1469
I'm new to android development, I was making this app that receives messages, onReceive
. I want to update those data in the ListView
that is in a fragment
and the update should happen in the background. Broadcast Receiver is registered globally
@Override
public void onReceive(Context context, Intent intent) {
Hover.initialize(context);
/* IncomingSMSReceiver incomingSMSReceiver = new IncomingSMSReceiver();
incomingSMSReceiver.onReceive(context,intent);*/
String uuid = intent.getStringExtra("response_message");
Log.d(TAG, " " + uuid);
if (intent.hasExtra("transaction_extras")) {
HashMap t_extras = (HashMap) intent.getSerializableExtra("transaction_extras");
/*if (t_extras.containsKey("confirmCode")) {
String confirmationCode = t_extras.get("confirmCode").toString();
Log.d(TAG, " "+confirmationCode);
}*/
if (t_extras.containsKey("Tsh")) {
String balance = t_extras.get("Tsh").toString();
Log.d(TAG," "+balance);
Bundle bundle = new Bundle();
bundle.putString("id", uuid);
bundle.putString("message", balance);
FragmentHistory fragmentHistory = new FragmentHistory();
fragmentHistory.setArguments(bundle);
}
}
}
Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// return inflater.inflate(R.layout.fragment_history, container,false);
View view = inflater.inflate(R.layout.fragment_history, container, false);
/* mRecyclerView =(RecyclerView) view.findViewById(R.id.recyclerView_history);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setHasFixedSize(true);
configAdapter();*/
dataSaver.initializeDataSaver(getActivity());
// listScore = view.findViewById(R.id.list);
return view;
}
@Override
public void onActivityCreated( Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//dataSaver.setViewList("Hello", "Hola");
dataSaver.getDataList(this.getActivity());
/*try {
Bundle bundle = getActivity().getIntent().getExtras();
dataSaver.setViewList(bundle.getString("id"), bundle.getString("message"));
} catch (Exception e) {
e.printStackTrace();
}*/
}
@Override
public void onResume() {
super.onResume();
dataSaver.getDataList(this.getActivity());
}
@Override
public void onPause() {
super.onPause();
Bundle arguments = getArguments();
if (arguments != null) {
handleArguments(arguments);
}
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
handleExtras(extras);
}
}
@Override
public void onStop() {
super.onStop();
Bundle arguments = getArguments();
if (arguments != null) {
handleArguments(arguments);
}
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
handleExtras(extras);
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
Bundle arguments = getArguments();
if (arguments != null) {
handleArguments(arguments);
}
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
handleExtras(extras);
}
Toast.makeText(getActivity(), "Destroyed", Toast.LENGTH_LONG).show();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Bundle arguments = getArguments();
}
private void handleArguments(Bundle arguments) {
// TODO
try {
String id =arguments.getString("id");
String message = arguments.getString("message");
dataSaver.setViewList(id, message);
} catch (Exception e) {
e.printStackTrace();
}
}
ListView Adapter
private ArrayList<TransDetails> scores;
TransDetails transDetails;
private MySharedPreference sharedPreference;
private ListView listScore;
private HashSet<String> scoreset;
private Gson gson;
public void initializeDataSaver(Activity activity){
transDetails = new TransDetails();
scores = new ArrayList<>();
gson = new Gson();
sharedPreference = new MySharedPreference(activity);
getHighScoreListFromSharedPreference();
}
/*public void setGson(Gson gson) {
this.gson = gson;
}*/
public Gson getGson() {
return gson;
}
/*public void setActivity(Activity activityf){
activityf = getActivity();
}
public Activity getActivity(){
return getActivity();
}*/
public void getHighScoreListFromSharedPreference() {
//retrieve data from shared preference
String jsonScore = sharedPreference.getHighScoreList();
Type type = new TypeToken<List<TransDetails>>(){}.getType();
scores = gson.fromJson(jsonScore, type);
if (scores == null) {
scores = new ArrayList<>();
}
}
public void saveScoreListToSharedpreference(ArrayList<TransDetails> scoresList) {
//convert ArrayList object to String by Gson
String jsonScore = gson.toJson(scoresList);
//save to shared preference
sharedPreference.saveHighScoreList(jsonScore);
}
public void upDateList(final String id, final String message){
transDetails.setId(id);
transDetails.setId(message);
scores.add(0, transDetails);
}
public void setViewList(String trans_id, String trans_amount){
if (trans_id != null) {
transDetails.setId(trans_id);
transDetails.setMessage(trans_amount);
scores.add(0,transDetails); //add to scores list
saveScoreListToSharedpreference(scores);
}
}
public void getDataList(Activity activity){
listScore = activity.findViewById(R.id.list);
if (scores.size() == 0) {
Toast.makeText(activity, "No data in sharedPreferences", Toast.LENGTH_SHORT).show();
} else {
getHighScoreListFromSharedPreference();
//get data
//set adapter for listview and visible it
ListViewAdapter adapter = new ListViewAdapter(activity, scores);
listScore.setAdapter(adapter);
}
}
Upvotes: 1
Views: 315
Reputation: 303
Fragments can't work standalone and must be in an activity.
so first step is create an Activity and put your fragment in it. then you must start that activity with an intent into your broadcast receiver. something like that:
context.startActivity(new Intent(this,ListActivity.class));
the other issue is that when new message receive your startActivity will launch again and create repeatedly. for avoid this issue you must add a flag to your Intent in this way:
context.startActivity(new Intent(this,ListActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
this flag is for avoid from recreating activity and bring to front existing one.
in this way the intent and data you passed to that will receive in onNewIntent()
method of activity and you can handle your data there.
so instead of setArguments in fragment you must start intent with bundle:
context.startActivity(new Intent(this,ListActivity.class).putExtras(bundle).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
Upvotes: 1