当前位置: 首页 > 知识库问答 >
问题:

从另一个活动恢复片段

时仰岳
2023-03-14

我有两个活动,分别是家庭活动和沙拉菜单活动。主页活动包含七个片段,其中一个名为菜单类别的片段有四个图像,第一个图像下面的文本是沙拉菜单,当用户单击沙拉菜单时,将启动名为沙拉菜单活动的新活动。

我想,当用户点击后退按钮时,home活动必须开始,并且应该从启动salad菜单活动的相同片段开始,即home活动中的菜单类别片段。我希望每个活动都会发生这种情况,例如,如果用户从您的订单片段开始一个新的活动,当他再次单击后退按钮时,您的订单片段必须恢复,等等,所有其他片段和活动。

家庭活动。Java语言

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

    if(SharedPrefManager.getInstance(this).isLoggedIn()){
        startActivity(new Intent(this, SignInActivity.class));
        finish();
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    Fragment fragment = new HomeFragment();
    if(fragment != null){

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.screen_area, fragment);

        fragmentTransaction.commit();

    }

}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }else if (id == R.id.action_signout){
        SharedPrefManager.getInstance(this).logout();
        finish();
        startActivity(new Intent(this, SignInActivity.class));
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.

    Fragment fragment = null;

    int id = item.getItemId();

    if (id == R.id.nav_home) {
        Toast.makeText(this, "Home was clicked", Toast.LENGTH_SHORT).show();
        fragment = new HomeFragment();
    } else if (id == R.id.nav_menu_categories) {
        Toast.makeText(this, "Menu categories was clicked", Toast.LENGTH_SHORT).show();
        fragment = new MenuCategoriesFragment();
    } else if (id == R.id.nav_your_orders) {
        Toast.makeText(this, "Your orders was clicked", Toast.LENGTH_SHORT).show();
        fragment = new YourOrdersFragment();
    } else if (id == R.id.nav_your_favorites) {
        Toast.makeText(this, "Your favorite was clicked", Toast.LENGTH_SHORT).show();
        fragment = new YourFavoritesFragment();
    } else if (id == R.id.nav_hot_deals) {
        Toast.makeText(this, "Hot deals was clicked", Toast.LENGTH_SHORT).show();
        fragment = new HotDealsFragment();
    } else if (id == R.id.nav_notifications) {
        Toast.makeText(this, "Notifications was clicked", Toast.LENGTH_SHORT).show();
        fragment = new NotificationsFragment();
    } else if(id == R.id.nav_profile){
        Toast.makeText(this, "Profile was clicked", Toast.LENGTH_SHORT).show();
        fragment = new ProfileFragment();
    } else if(id == R.id.nav_logout){
        Toast.makeText(this, "Logout was clicked", Toast.LENGTH_SHORT).show();
        SharedPrefManager.getInstance(this).logout();
        startActivity(new Intent(HomeActivity.this, SignInActivity.class));
        finish();
    }

    if(fragment != null){

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.screen_area, fragment);

        fragmentTransaction.commit();

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

MenuCategoriesFragment。Java语言

private LinearLayout linearLayoutSaladMenu, linearLayoutNoddlesCategories, linearLayoutPotatoMenu, linearLayoutBurgerMenu;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_menu_categories, null);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getActivity().setTitle("Menu Categories");

    linearLayoutSaladMenu = (LinearLayout) view.findViewById(R.id.salad_menu);
    linearLayoutNoddlesCategories = (LinearLayout) view.findViewById(R.id.noddles_categories);
    linearLayoutPotatoMenu = (LinearLayout) view.findViewById(R.id.potato_menu);
    linearLayoutBurgerMenu = (LinearLayout) view.findViewById(R.id.burger_menu);

    linearLayoutSaladMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(getActivity(), SaladMenuActivity.class));
        }
    });

    linearLayoutNoddlesCategories.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "Noddles categories was clicked", Toast.LENGTH_SHORT).show();
        }
    });

    linearLayoutPotatoMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "Potato menu was clicked", Toast.LENGTH_SHORT).show();
        }
    });

    linearLayoutBurgerMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "Burger menu was clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

SaladMenuActivity。班

List<SaladMenuItem> saladMenuItemsList;
ListView listViewSaladMenu;

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

    saladMenuItemsList = new ArrayList<>();
    listViewSaladMenu = (ListView) findViewById(R.id.listViewSaladMenu);

    saladMenuItemsList.add(new SaladMenuItem(R.drawable.fried_rice_with_sauce, "Fried Rice with Sauce", "$30.00"));
    saladMenuItemsList.add(new SaladMenuItem(R.drawable.vegetable_salad, "Vegetable Salad", "$12.00"));

    SaladMenuListAdapter adapter = new SaladMenuListAdapter(this, R.layout.salad_menu_list, saladMenuItemsList);

    listViewSaladMenu.setAdapter(adapter);

}

@Override
public void onBackPressed() {
    super.onBackPressed();
}

共有1个答案

邓焱
2023-03-14

试着在你想去的地方开始活动。。使用onBackPressed()。我认为这会解决你的问题

 类似资料:
  • 我需要拍照并发送到主活动并发送到第三活动 这是MainActivity的代码 我有任何错误,但当我试图拍摄一张照片时,我得到这样的信息:不幸的是,第一相机停止了,我的问题是什么?

  • 我有一个活动A,它使用一个意图去活动B,有时活动A也开始另一个活动,活动C,当这些活动结束时,活动A有办法知道它是从哪个活动(B或C)重新开始的吗?提前感谢。

  • 我的Android应用程序有问题,我正在使用Android StudioIDE进行开发。差不多是当我把应用程序放在后台几分钟,或者被系统杀死,或者我混合了片段的不同布局时。我在下面放了一张图片: 如果你有其他人也写的话,我已经尝试了各种方法。提前谢谢你。

  • 问题内容: 我在应用程序的状态栏中有一条通知: 这样做的问题是,当您从应用程序中按下主页按钮(将其推到后台)然后在从状态栏访问的列表中的通知上按下时,它将启动活动的新副本。我要做的就是恢复应用程序(例如,长按主屏幕按钮并按应用程序图标上的)。有没有一种创建意图的方法? 问题答案: 我已通过将我的活动更改为androidManifest.xml文件来解决了此问题。 此属性的默认值为,它允许运行任意数

  • 问题内容: 从Activity中包含的RecyclerView的适配器中,我试图在按下RecyclerView的元素时启动一个片段,这是我现在的代码: 我测试了它是否启动了我创建的一些“测试活动”,所以我知道除了片段启动之外的所有功能都可以正常工作。 错误在这里: 我正在启动作为活动的Fragment,所以当我运行应用程序时,它崩溃了,并告诉我在清单中将MainFragment声明为活动。 如何从