从另一个活动中恢复碎片

问题描述 投票:0回答:1

我有两个名为Home activity和Salad Menu活动的活动。 Home Activity包含七个片段,其中一个片段名为Menu Categories Fragment有四个图像,第一个图像下面的文本是Salad菜单,当用户点击沙拉菜单时,新的活动名为Salad Menu Activity。

我想要的是,当用户点击后退按钮时,家庭活动必须开始,并且相同的片段应该从哪个沙拉菜单活动开始,这是家庭活动中的菜单类别片段。我希望每次活动都能发生这种情况,例如,如果用户从您的订单片段中启动新活动,并且当他再次单击后退按钮时,您的订单片段必须重新开始,依此类推,其他所有片段和活动。

主页Activity.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;
}

menu categories fragment.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.class

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();
}
java android android-studio android-fragments
1个回答
0
投票

尝试onBackPressed()onBackPressed()内部开始您想去的活动..使用onBackPressed()。我认为这将解决您的问题

© www.soinside.com 2019 - 2024. All rights reserved.