Reputation: 21
My app is a message app and it have an activity that perform inbox functionality. So, in my inbox i will list unread messages on top and read messages at bottom.
So if i click on an unread message it will move to new activity which displays the particular message selected and in my database i will mark it as read. So, on clicking back, when it moves to the previous activity this message should be displayed among the read ones. But, in my case it is showing the old scenario i.e, the message is still displayed among unread.
I thought that the activity should be refreshed when it returns so i tried some refreshing methods like:
1.
public void onResume(Bundle s)
{ // After a pause OR at startup
super.onResume();
this.onCreate(s);
}
2.
Intent intent = new Intent(this, msgdisplayActivity.class); //msgdisplayActivity is activity which display the selected message.
//intent.putExtra("someData", "Here is some data");
startActivityForResult(intent, 1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
Intent refresh = new Intent(this, inboxlist.class); //inboxlist is activity which list the read and unread messages
startActivity(refresh);
this.finish();
}
}
But both of this didn't. onResume()
is not being called and the other one shows error.
Upvotes: 1
Views: 7552
Reputation: 1324
Your code snippet 1 will create a recursive scenario here. Read about Activity Lifecycle for more details.
If you need to refresh your data dynamically, move the data fetching and showing on the UI code portion to your onResume method. So that data is fetched every time the activity resumes.
Otherwise, you can use LiveData to observe the changes from the database and automatically update the UI.
Upvotes: 0
Reputation: 5347
Your onResume()
method is never called because of its signature.
It is a good idea to use @Override
whenever you intend to override a method. Had you used @Override
in your code, you would have been pointed towards the problem that you intend to override but don't succeed.
Judging from your point 1
I'd recommend that you try to understand Activity lifecycle better, because calling super.onCreate()
from within onResume()
and also after super.onResume()
is, hmm, all wrong.
Your first code snippet should look like this:
@Override
protected void onResume() {
super.onResume();
// do what you need to do if your activity resumes
}
But you should make sure you understand when onResume()
gets called.
Regarding your onActivityResult()
implementation, it may not be the most efficient approach to re-load the "inbox" Activity, but it should work. The code looks good except for one aspect: In this method, I would not call super.onActivityResult()
because it's not required and it cannot contribute something meaningful. I never do it and that works fine. So maybe get ridd of this call and see whether the error still occurs.
If yes, please post the error.
Upvotes: 0
Reputation: 2183
you can put your refresh layout code in one method
e.g.
void populatedata()
{
//your code to show data in listview or lables
//if you are using listview, then you can write adapter.notifyDataSetChanged(); to refresh list
}
then, you can use
Intent intent = new Intent(this, msgdisplayActivity.class); //msgdisplayActivity is activity which display the selected message.
startActivityForResult(intent, 1);
and
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
//call populatedatamethod
populatedata();
}
}
the same method you can use in onCreate method.
Can you please provide a your onCreate and onResume code? so that i can edit my answer as per your code?
onResume is also called just after onActivityResult(), so you can also put your code there
Upvotes: 0
Reputation: 1
different devices handle activity destruction differently. the sure method i've found is saving state to app db and retrieving on focus of app.
Upvotes: -1