views:

82

answers:

2

I have two activities

The first one gets some data from a content provider, and displays it

The second activity has a button, and when clicked it should call the first activity to "refresh", in other words to reload data from the content provider

This is my first activity

...
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.contactsview);
            // Grab data and put it onto screen
                getDataAndPutToUI();
    }

    @Override
    public void onStart()
    {
       //Refresh data
           getDataAndPutToUI();
    }
...

And this is my second activity

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.optionsview);

    Button button = (Button) findViewById(R.id.intentButton);
        button.setOnClickListener(this);

}   

// This is bound to the "refresh" button
    @Override
public void onClick(View v) {
    // Call first activity, to reload the contacts
            Intent i = new Intent(this, FirstActivity.class);
    startActivity(i);

}

Is this the correct way to be implementing such functionality? It feels incorrect, but I can't determine any reasons to backup my claims

+2  A: 

hi.. i u can do it by setting flags for your intent try like below,go through other flags

Intent i = new Intent();
                            i.setClass(this, Rss_Feed.class);
//                          i.setClass(Rss_Feed.this, Bru_Maps.class);
                            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MGS
+1  A: 

It depends what behavior you are looking for. As you have currently implemented it, the first Activity will be refreshed any time that it is displayed to the user. For example, if the user pressed the home button from the first activity and later returned to the first activity via a long press on home, then the first activity would refresh it's data.

If that is what you want, then you're doing it right.

If you only want the first activity to refresh when you press the refresh button on the second activity, then you need to use your Intent to do that. One way would be to add a boolean extra to your Intent:

i.putExtra("reloadContent", true);

Then check for this extra in the onStart menthod.

Computerish
So you set an option on the intent, then in the onStart method you check for that, and if true you do something. Is this how Google intended for us to refresh things or is there another way? Thanks
James.Elsey
Yes, that is correct. I am not 100% sure if the "right" way of doing things, but I suspect it is. One of the common intended uses of intents is to update an Activity that new data has been downloaded/is available, so I'm guessing that this usage is correct too. Generally if something works and is logical, then it is correct.
Computerish
Thanks, that works for me :)
James.Elsey