我有一个带有MainActivity
的Android应用程序,可以在按下按钮时将内容区域与片段交换。使用当前代码(如下所示),它可以成功
...但工具栏本身并不会更新以显示"抽屉" /"菜单" (汉堡包)按钮返回其原始状态(getSupportFragmentManager().getBackStackEntryCount() == 0
)。我想我需要在我的活动中实施FragmentManager.OnBackStackChangedListener
,尽管我的尝试并未如此成功。我也看到提到syncState()
,虽然我也没有太多的运气。
如何将工具栏主页按钮返回到"抽屉" /"菜单"交换和弹出片段后的外观和功能?
我的代码可以在下面找到。在分享之前试图简化它,希望我没有削减任何重要的东西。
MainActivity.java
:
package me.stashhh.stashboxes;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import me.stashhh.stashboxes.model.CountryManager;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
// normal onCreate stuff
super.onCreate(savedInstanceState);
// The ContentView that we're referencing
setContentView(R.layout.activity_main);
// Toolbar setup
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
// Drawer setup
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
toolbar,
R.string.action_drawer_open,
R.string.action_drawer_close
);
mDrawerLayout.setDrawerListener(mDrawerToggle); // REQUIRED FOR ARROW ANIMATION. Fml.
mDrawerToggle.syncState();
// Listen for changes in the back stack
getSupportFragmentManager().addOnBackStackChangedListener(this);
}
public void onBackPressed() {
// Catch back action and pops from backstack
// (if you called previously to addToBackStack() in your transaction)
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
// Default action on back pressed
super.onBackPressed();
}
}
public boolean onOptionsItemSelected(MenuItem item) {
// Get item selected and deal with it
switch (item.getItemId()) {
case android.R.id.home:
//called when "up" (back button) is called in toolbar
onBackPressed();
return true;
}
return true; // default return since 'boolean'; not sure if proper?
}
/**
* Called when the user clicks the "View Stashboxes" button
*/
public void viewStashboxes(View view) {
// update the main content by replacing fragments
Fragment stashboxesFragment = new StashboxesFragment();
Bundle args = new Bundle();
args.putString(StashboxesFragment.ARG_TEST_STRING, "Testing 1.. 2.. 3..");
stashboxesFragment.setArguments(args);
FragmentManager fragmentManager = getSupportFragmentManager();
// Add this transaction to the back stack...
// http://developer.android.com/training/implementing-navigation/temporal.html#back-fragments
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, stashboxesFragment, "stashboxesFragment");
fragmentTransaction.addToBackStack("stashboxesFragment");
fragmentTransaction.commit();
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If drawer is open, we have to close it...
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer); // The drawer layout
LinearLayout mDrawerList = (LinearLayout) findViewById(R.id.left_drawer); // The drawer "list" itself
mDrawerLayout.closeDrawer(mDrawerList); // Close the drawer list
}
}
/**
* Fragment that appears in the "content_frame"
*/
public static class StashboxesFragment extends Fragment {
public StashboxesFragment() {
// empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// REDACTED CODE FOR SIMPLICITY.
// inflate a layout in the main content area.
return rootView;
}
}
}
我是Android开发的新手,所以您可能会有任何其他批评。谢谢。
答案 0 :(得分:2)
这对我有用:请更新您的onBackPressed()
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
if(frgManager.getBackStackEntryCount()>0){
getSupportFragmentManager().popBackStack();
// shows the drawer icon when on moving back
mDrawerToggle.setDrawerIndicatorEnabled(true);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
}else{
super.onBackPressed();
}
mDrawerToggle.syncState();
}