片段关闭后使用OnResume更新活动

时间:2017-10-25 22:36:42

标签: android android-fragments onresume dismiss

我有一个片段,可以将数据添加到我的数据库中。当我用 dismiss()关闭它时,它会返回我的活动。我想在那次活动中更新我的recyclerView。

我对活动生命周期的理解是onResume应该被称为正确吗?我有一个登录我的onResume方法,据我所知,它没有被调用。

什么是更好的解决方案,为什么没有被调用?

的onResume

  @Override
public void onResume() {
    super.onResume();
    Log.e("Resume", "Resuming");

}

我的片段中的按钮单击侦听器。这里的日志工作得很好。

  //save button for saving workouts
    mButton2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            String title = mEditText.getText().toString();
            String category = mSpinner.getSelectedItem().toString();
            String weight = mEditText2.getText().toString();
            int icon = buildIcon(category);

            //pass all the data into a new object for the Recycler View to store and show also refresh
            db.addWorkout(new Workout(title, weight, "8/8/8", icon));
            Log.e("Database Build", "Added a new workout: " + title);

            dismiss();
        }
    });

1 个答案:

答案 0 :(得分:2)

当您开始处理片段时,活动从未暂停。 onResume在这种情况下不会被调用,并且预计会出现预期的生命周期行为。

你应该考虑实现某种类型的回调,以便在Fragment关闭时让Activity知道。 android documentation对如何与片段进行通信有一个非常好的解释。使用文档中的模式并自己构建OnFragmentClosedListener