Android的;我怎么能初始化状态在一个活动,然后让另外一个刷新? [英] Android; How can I initialise state in one activity, then have another refresh that?

查看:248
本文介绍了Android的;我怎么能初始化状态在一个活动,然后让另外一个刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动

第一个得到从内容提供了一些数据,并显示它

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

这是我的第一个活动。

...
@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

@覆盖     公共无效的onCreate(包savedInstanceState){         super.onCreate(savedInstanceState);          的setContentView(R.layout.optionsview);

@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

推荐答案

这要看你在找什么行为。作为当前已付诸实施,所述第一活动将被刷新,它被显示给用户的任何时间。例如,如果用户$ P $的第一个活动pssed home键,后来通过在离家很远preSS回到了第一个活动,那么第一个活动将刷新它的数据。

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.

如果你只想要第一个活动,当你preSS的第二个活动的刷新按钮刷新,那么你需要用你的意图来做到这一点。一种方法是将一个布尔额外添加到您的意图:

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);

然后检查这一额外的ONSTART menthod。

Then check for this extra in the onStart menthod.

这篇关于Android的;我怎么能初始化状态在一个活动,然后让另外一个刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆