【问题标题】:Resume Fragments from another activity从另一个活动中恢复片段
【发布时间】:2018-03-07 02:28:21
【问题描述】:

我有两个活动,分别名为 Home 活动和 Salad Menu 活动。 Home Activity 包含七个片段,其中一个名为 Menu Categories Fragment 的片段有四个图像,第一个图像下方的文本是沙拉菜单,当用户单击沙拉菜单时,将启动名为沙拉菜单 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;
}

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.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();
}

【问题讨论】:

  • 使用EventBus 或本地Broadcast

标签: java android android-studio android-fragments


【解决方案1】:

尝试onBackPressed() 并在onBackPressed() 中启动您想去的活动..使用onBackPressed()。我想这会解决你的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多