简体   繁体   English

Android Studio:在活动中调用“finish()”后,前一屏幕中的代码无法继续

[英]Android Studio: Code in previous screen not continuing after calling “finish()” in activity

I'm new to Android Development and am trying to make a simple habit tracking app.我是 Android Development 的新手,正在尝试制作一个简单的习惯跟踪应用程序。 The initial screen is a fragment called HabitFragment.初始屏幕是一个名为 HabitFragment 的片段。 It has a button that, when clicked, takes the user to an activity, called HabitDialogueActivity, where they create a new habit that they would like to keep track of.它有一个按钮,当单击该按钮时,用户会进入一个名为 HabitDialogueActivity 的活动,在那里他们会创建一个他们想要跟踪的新习惯。 When the "done" button is pressed, I call the "finish()" method.当按下“完成”按钮时,我调用“finish()”方法。 In the app, this takes me back to the original screen.在应用程序中,这会将我带回原始屏幕。 However, my code does not seem to continue as my log entries do not show up.但是,我的代码似乎没有继续,因为我的日志条目没有出现。

I have this onClickListener and some code after it, it is inside of my OnCreateView():我有这个 onClickListener 和一些代码,它在我的 OnCreateView() 中:

Button newHabitButton = view.findViewById(R.id.newHabitButton);
newHabitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            goToDialogue();
        }
    });
if (HabitDialogueActivity.getHabitListSize() > habitListSize) {
    Log.d("TAG", "adding to linearLayout, am in if statement");
    habitListSize = HabitDialogueActivity.getHabitListSize();
    Habit newHabit = HabitDialogueActivity.habitList.get(habitListSize - 1);
    TextView text = new TextView(this.getContext());
    text.setText(newHabit.habitName);
    text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    linearLayout.addView(text);
} else {
    Log.d("TAG", "habit list size not changed");
}

and this is my goToDialogue():这是我的 goToDialogue():

private void goToDialogue() {
    Log.d("TAG", "HabitFragment goToDialogue");
    Intent intent = new Intent(HabitFragment.this.getActivity(), HabitDialogueActivity.class);
    startActivity(intent);
}

My HabitDialogueActivity just has a few text fields, an ArrayList of habits, and a "done" button which calls finish().我的 HabitDialogueActivity 只有几个文本字段,一个 ArrayList 的习惯,以及一个调用完成()的“完成”按钮。 It takes me back to my HabitFragment, but the Log entries in the if statement that follow the setOnClickListener do not show in the log.它带我回到我的 HabitFragment,但是 setOnClickListener 后面的 if 语句中的日志条目没有显示在日志中。 Why doesn't the code in HabitFragment continue to execute after I leave HabitDialogueActivity?为什么离开 HabitDialogueActivity 后 HabitFragment 中的代码没有继续执行?

I am new to this website, so I'm sorry if I've left anything out.我是这个网站的新手,所以如果我遗漏了任何内容,我很抱歉。

Put your code inside onResume to rerun it when you go back to previously screen and remove it from on Create View.将您的代码放入 onResume 以在您 go 回到之前的屏幕并将其从创建视图中删除时重新运行它。 onResume will run after oncreate when you open the screen for the first time.当您第一次打开屏幕时,onResume 将在 oncreate 之后运行。

Check the docs https://developer.android.com/guide/components/activities/activity-lifecycle检查文档https://developer.android.com/guide/components/activities/activity-lifecycle

@Override
public void onResume() {
    super.onResume();
    // put your code here...
    if (HabitDialogueActivity.getHabitListSize() > habitListSize) {
        Log.d("TAG", "adding to linearLayout, am in if statement");
        habitListSize = HabitDialogueActivity.getHabitListSize();
        Habit newHabit = HabitDialogueActivity.habitList.get(habitListSize - 1);
        TextView text = new TextView(this.getContext());
        text.setText(newHabit.habitName);
        text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        linearLayout.addView(text);
    } else {
        Log.d("TAG", "habit list size not changed");
    }
}

OnCreateView is only called when the Fragment is created. OnCreateView仅在创建 Fragment 时调用。 Since it wasn't destroyed when you navigated to the HabitDialogueActivity , OnCreateView isn't called again.由于导航到HabitDialogueActivity时它没有被销毁,因此不会再次调用OnCreateView

You can move your if statement inside the Fragment's onResume() method in order to check if new data is available each time you navigate back to the activity to which the Fragment is attached.您可以在 Fragment 的onResume()方法中移动 if 语句,以便在每次导航回 Fragment 所附加到的活动时检查新数据是否可用。

    @Override
    public void onResume() {
        super.onResume();
        if (HabitDialogueActivity.getHabitListSize() > habitListSize) {
            Log.d("TAG", "adding to linearLayout, am in if statement");
            habitListSize = HabitDialogueActivity.getHabitListSize();
            Habit newHabit = HabitDialogueActivity.habitList.get(habitListSize - 1);
            TextView text = new TextView(this.getContext());
            text.setText(newHabit.habitName);
            text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            linearLayout.addView(text);
        } else {
            Log.d("TAG", "habit list size not changed");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM