1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Saans
  • 21
  • 1
  • 7

4 Answers4

0

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

Akbari Dipali
  • 2,173
  • 1
  • 20
  • 26
  • Thank you my onActivityResult worked but still onResume is not working. i wrote a toast inside onResume function to check whether it is being called. Toast was not displayed, that means it is not called. can u help me? – Saans Mar 09 '13 at 15:14
  • you can add you code in one common method and use it in onCreate as well as onActivityResult() method as i have wrote here, if you are having any doubts, please paste your onCreate and onResume code here, so I can write full code – Akbari Dipali Mar 09 '13 at 15:30
0

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.

class stacker
  • 5,357
  • 2
  • 32
  • 65
0

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.

Eishon
  • 1,274
  • 1
  • 9
  • 17
-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.

  • While it is true that the actual destruction depends on the available memory, the life cycle is well-defined. The OP made a terrible mistake in his code snippet which is worth pointing out, while he does not mention that he has a persistency problem. Please make sure your answer relates to the question. – class stacker Mar 09 '13 at 14:39