我一直在阅读有关片段和片段返回处理的信息,但在这里仍然找不到最佳解决方案。
我有导航抽屉。
当我点击 item1 时,假设我的导航抽屉列表中有 2 个项目(item1,item2),它打开了 A Fragment。点击in按钮后在A Fragment中打开B Fragment。因此,当打开 B 片段并从导航抽屉中选择 item2 时,将打开 C 片段。
现在,当我按下后退按钮时,它应该完成活动而不是转到 B 片段。
所以一句话,当我选择 item1 时,item2 打开 A 片段或 C 片段,按下后退按钮后应该完成活动,但是当打开 B 片段(在 item1 下)时,它必须回到 A 片段
请帮助这对我来说非常困难。谢谢
public class MainActivity extends DefaultActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggleWrapper mDrawerToggle;
private ListView mDrawerListView;
private TextView mTitle;
private Runnable mPendingRunnable;
private Handler mHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDrawerListView = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mFragmentManager = getSupportFragmentManager();
mHandler = new Handler();
LayoutInflater inflator = (LayoutInflater) this .getSystemService(LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.custom_view, null);
mTitle = (TextView)v.findViewById(R.id.title);
getSupportActionBar().setBackgroundDrawable(null);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setIcon(R.drawable.button_indicator_menu);
getSupportActionBar().setCustomView(v);
v.findViewById(R.id.menu).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!mDrawerLayout.isDrawerOpen(GravityCompat.START)){
mDrawerLayout.openDrawer(GravityCompat.START);
}else {
mDrawerLayout.closeDrawers();
}
}
});
mDrawerToggle = new ActionBarDrawerToggleWrapper(this, mDrawerLayout, R.drawable.transparent_gic, R.string.drawer_open, R.string.drawer_close){
public void onDrawerClosed(View view) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
if (mPendingRunnable != null) {
mHandler.post(mPendingRunnable);
mPendingRunnable = null;
}
}
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.addPartnerToggle(new ContentDisplaceDrawerToggle(this, mDrawerLayout, R.id.content_frame));
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
setLeftDrawerLeftMenu();
getFragment(getString(R.string.home));
setTitle(getString(R.string.home));
}
private void setLeftDrawerLeftMenu(){
final ArrayList<Item> items = new ArrayList<Item>();
items.add(new SectionItem(this,"icons_small_home",getString(R.string.home)));
items.add(new SectionItem(this,"icons_small_myacounts",getString(R.string.my_account)));
items.add(new EntryItem(getString(R.string.bonuses)));
items.add(new EntryItem(getString(R.string.account_recharge)));
items.add(new EntryItem(getString(R.string.tariff_plans)));
items.add(new EntryItem(getString(R.string.languages)));
EntryAdapter adapter = new EntryAdapter(MainActivity.this, items);
mDrawerListView.setAdapter(adapter);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
EntryItem item = (EntryItem) items.get(i);
setTitle(item.getTitle());
}
});
}
public void setTitle(final String title){
mTitle.setText(title);
mPendingRunnable = new Runnable() {
@Override
public void run() {
getFragment(title);
}
};
mDrawerLayout.closeDrawers();
}
public void getFragment(String name){
if(getString(R.string.orange_shops).equalsIgnoreCase(name)){
if (!(mSelectedFragment instanceof OrangeShopsFragment)){
addPage(new MyFragment(MainActivity.this),true);
}
}else if(getString(R.string.home).equals(name)){
addPage(new HomeFragment(MainActivity.this),false);
}else{
throw new RuntimeException("unknown fragment "+ name);
}
}
public void addPage(final DefaultFragment pDefaultFragment, final boolean isAddToBackStack){
FragmentTransaction transaction = mFragmentManager.beginTransaction();
transaction.replace(R.id.content_frame, pDefaultFragment);
if (isAddToBackStack) transaction.addToBackStack(null);
transaction.commitAllowingStateLoss();
}
}