java Android,在同一活动中返回之前调用的方法

dz6r00yl  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(166)

我正在Android Studio中编写Java,目前正在制作一个日历应用程序,可以保存、显示和编辑事件。我必须添加一些视图,如MonthView WeekView和DailyView。起初,我将其设置为所有活动,当我想返回时,按下后退按钮几乎完成了我的工作。由于一些可能性,我原来只保留了一个活动,并用一些方法做同样的工作,而不是制作活动。我有一个导航抽屉,所以在onItemNavigationClick中用户可以选择想要的视图。所以,我的问题是我找不到执行前一个方法的方法,就像如果我转到周视图并按下后退按钮,回到月视图或日视图。
因为我认为backpressed是回到一个活动,所以我认为它不能帮助我。我会感激任何帮助。

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    switch (item.getItemId()) {
        case R.id.menuSchedule:
            setAllEvents();
            drawerLayout.closeDrawer(GravityCompat.START);

            break;
        case R.id.daysView:
            setDaily();
            drawerLayout.closeDrawer(GravityCompat.START);

            break;

        case R.id.weekView:

            setWeek();
            drawerLayout.closeDrawer(GravityCompat.START);

            break;

        case R.id.monthView:
            setMonthView();
            drawerLayout.closeDrawer(GravityCompat.START);

            break;
        case R.id.refreshItem:
            finish();
            startActivity(getIntent());
            break;
        case R.id.syncItem:
            break;

        default:
            onNavigationItemSelected(item);
    }

    return true;
}
    • 我的set方法如何工作的示例:**
private void setMonthView() {
        monthYearText.setText(monthYearFromDate(CalendarUtils.selectedDate));
        ArrayList<LocalDate> daysInMonth = daysInMonthArray();

        CalendarAdapter calendarAdapter = new CalendarAdapter(daysInMonth, this, getApplicationContext());
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);

        calendarRecyclerView.setLayoutManager(layoutManager);

        calendarRecyclerView.setAdapter(calendarAdapter);
        ViewGroup.LayoutParams params = calendarRecyclerView.getLayoutParams();
        params.height=1500;
        calendarRecyclerView.setLayoutParams(params);

        monthListView.setVisibility(View.GONE);
        monthYearText.setVisibility(View.VISIBLE);
        daysOfWeekDaily.setVisibility(View.GONE);
        daysOfWeek.setVisibility(View.VISIBLE);
        prevMonth.setVisibility(View.VISIBLE);
        nextMonth.setVisibility(View.VISIBLE);
        calendarRecyclerView.setVisibility(View.VISIBLE);
        nestedScrollView.setVisibility(View.VISIBLE);

    }
    • 解决方案**

感谢大卫韦瑟,代码的工作方式,我想,这里的源代码我的问题:

public void onMyBackPressed() {
        // Pop current view type off the stack
        stack.removeFirst();
        // Check the previous view type
        String previousViewType = stack.peekFirst();
        if (previousViewType == null) {
            // Nothing to go back to, so finish this Activity
            super.onBackPressed();
            return;
        }
        if (previousViewType.equals("daily")) {
            setDaily();
        } else if (previousViewType.equals("week")) {
            setWeek();
        } else if (previousViewType.equals("all"))
        {
            setAllEvents();
        }else if (previousViewType.equals("month"))
        {
            setMonthView();
        }
    }



 @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {
            case R.id.menuSchedule:
                setAllEvents();
                stack.addFirst("all");
                drawerLayout.closeDrawer(GravityCompat.START);

                break;
            case R.id.daysView:
                setDaily();
                stack.addFirst("daily");
                drawerLayout.closeDrawer(GravityCompat.START);

                break;

            case R.id.weekView:

                setWeek();
                stack.addFirst("week");
                drawerLayout.closeDrawer(GravityCompat.START);

                break;

            case R.id.monthView:
                setMonthView();
                stack.addFirst("month");
                drawerLayout.closeDrawer(GravityCompat.START);

                break;
            case R.id.refreshItem:
                finish();
                startActivity(getIntent());
                break;
            case R.id.syncItem:
                break;

            default:
                onNavigationItemSelected(item);
        }

        return true;
    }
h5qlskok

h5qlskok1#

您可以通过构建一个堆栈来表示您正在显示的视图类型来实现这一点。无论何时用户从导航抽屉式菜单中选择某项内容,调用方法来显示视图并将一些内容压入堆栈(StringInteger常量),它表示您正在显示的视图类型。然后重写onBackPressed(),以便取代默认行为(也就是完成当前的Activity并返回到前一个Activity),你这样做:

  • 弹出堆栈中的最后一项(这表示当前显示的视图)
  • 如果堆栈中没有剩余内容,则应该使用super.onBackPressed()调用默认行为,这将使用户返回到HOME屏幕或其他屏幕
  • 否则,检查堆栈中最上面的项(它代表先前显示的视图),并使用它调用适当的方法来显示它所代表的视图。在这种情况下,请记住不要将新项推到堆栈中,因为堆栈中的最上面项已经代表了正在显示的视图。

代码示例:

// Declare the stack as a member variable in the Activity
ArrayDeque<String> stack = new ArrayDeque<String>();

switch语句中,每个不同视图类型如下所示:

case R.id.daysView:
        setDaily();
        // push the current view type onto the stack
        stack.addFirst("day");
case R.id.weekView:
        setWeek();
        // push the current view type onto the stack
        stack.addFirst("week");
etc...

现在覆盖onBackPressed()的行为:

@Override
public void onBackPressed() {
    // Pop current view type off the stack
    stack.removeFirst();
    // Check the previous view type
    String previousViewType = stack.peekFirst();
    if (previousViewType == null) {
        // Nothing to go back to, so finish this Activity
        super.onBackPressed();
        return;
    }
    if (previousViewType.equals("day")) {
        setDaily();
    } else if (previousViewType.equals("week")) {
        setWeekly();
    } else ... // rest of the view types here
}

相关问题