如何在 "onResume" 上更新 Activity?

How update an Activity on "onResume"?

我有一个包含一些文本的列表视图。但是当列表视图为空时,它将显示一个按钮,将您带到一个新的 activity,您可以在其中键入一些文本并保存在 SharedPreferences 上并完成。我希望 Listview 的 Activity 在另一个关闭时更新。我尝试了 onResume() 但由于某种原因它会永远保持刷新状态。我该怎么做?
顺便说一句,我正在更新使用:
startActivity(getIntent()); finish();
有更好的方法吗?
谢谢。



编辑--------------------------------------------
正如@Alex Loper 所说,我在 onResume() 方法中添加了 notifyDataSetOnChange() 。但是它崩溃了这个日志:

12-31 19:45:05.284  11864-11864/com.nerv.cas.partying E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.nerv.cas.partying, PID: 11864
java.lang.RuntimeException: Unable to resume activity {com.nerv.cas.partying/com.nerv.cas.partying.YourPartiesActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2812)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2845)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2262)
        at android.app.ActivityThread.access0(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.nerv.cas.partying.YourPartiesActivity.onResume(YourPartiesActivity.java:48)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1215)
        at android.app.Activity.performResume(Activity.java:5327)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2797)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2845)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2262)
            at android.app.ActivityThread.access0(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

我的class是这样的:

ArrayAdapter<String> adapter;
User user;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_your_parties);

    user = getUserData();
    fillUserParty(user);
    fillAdapter(user);

    ListView partiesListView = (ListView) findViewById(R.id.parties_list_view);
    Button goToCreatePartyButton = (Button) findViewById(R.id.go_to_create_party_button);
    if(adapter != null){
        partiesListView.setVisibility(View.VISIBLE);
        partiesListView.setAdapter(adapter);
    }else{
        goToCreatePartyButton.setVisibility(View.VISIBLE);
        goToCreatePartyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(YourPartiesActivity.this, CreatePartyActivity.class));
            }
        });
    }
}

@Override
protected void onResume() {
    super.onResume();
    adapter.notifyDataSetChanged();
}

private void fillAdapter(User user){
    List<Party> parties = user.getParties();
    if(parties != null) {
        for (int i = 0; i < parties.size(); i++) {
            Party party = parties.get(i);
            adapter.add(party.getName());
        }
    }
}

private void fillUserParty(User user){
    SharedPreferences preferences = getSharedPreferences("Parties", MODE_PRIVATE);
    int numOfParties = CreatePartyActivity.checkNumberOfParties(preferences);
    for(int i=0; i<numOfParties; i++){
        String partyName = preferences.getString("Name" + i, "");
        if(partyName != ""){
            int id = preferences.getInt("Id" + i, 0);
            Party party = new Party(id, partyName);
            user.joinParty(party);
        }
    }
}

public User getUserData(){
    SharedPreferences preferences = getSharedPreferences("User", 0);
    int id = preferences.getInt("Id", 0);
    String username = preferences.getString("Username", "");
    String name = preferences.getString("Name", "");
    return new User(id, username, name);
}

onResume() 中调用列表视图适配器的 notifyDataSetChanged() 方法以使用新信息重绘列表视图。

编辑:根据发布的代码和堆栈跟踪,适配器在使用之前从未实例化,并导致 NPE。在调用 if(adapter != null){ 之前实例化适配器,以便正确填充它。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_your_parties);

    ListView partiesListView = (ListView) findViewById(R.id.parties_list_view);
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);

    user = getUserData();
    fillUserParty(user);
    fillAdapter(user);
    partiesListView.setAdapter(adapter);

    Button goToCreatePartyButton = (Button) findViewById(R.id.go_to_create_party_button);
    goToCreatePartyButton.setVisibility(View.VISIBLE);
    goToCreatePartyButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(YourPartiesActivity.this, CreatePartyActivity.class));
        }
    });

}

如果您使用数据库和 "contentprovider",您将能够使用 Loaders(LoaderCallback),这将有助于监控底层数据源的变化。

你会发现,当第二个 activity 开始时,第一个 activity 调用 onpause,然后在大约 10 秒后有时调用 ondestroy。因此,依靠 onresume 来管理状态可能会很紧张

我在发布的代码中找不到初始化适配器。我认为适配器总是空的!

要解决空指针异常,像这样更改 onResume 方法:

@Override
protected void onResume() {
  super.onResume();
  if (adapter != null)
     adapter.notifyDataSetChanged();
}