在活动之间切换 - >无限循环

时间:2014-01-17 01:48:28

标签: android

我是Android新手并且遇到“小问题”。

我正在开始扩展基类的主要活动。 基类启动我的“导航”类...并且在导航类中我有一个可选择的开关参数,应该创建活动 - 好吧,此时我有一个无限循环,我不知道为什么.. < / p>

当我使用片段但我想要使用活动时,它工作得很好...希望有人可以帮助我:)

这是我的代码。

public class MainActivity extends Base {

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.v("boot", "MainActivity-onCreate");
    super.onCreate(savedInstanceState);

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, new MainFragment())
            .commit();
}

public class Base extends ActionBarActivity implements
    NavigationDrawer.NavigationDrawerCallback {

public NavigationDrawer mNavigationDrawer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.v("boot", "Base-onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Set up the drawer
    mNavigationDrawer = (NavigationDrawer)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

    mNavigationDrawer.setUp((DrawerLayout)findViewById(R.id.drawer_layout),
            R.id.navigation_drawer);
}

导航

public class NavigationDrawer extends Fragment {

private ActionBarDrawerToggle mDrawerToggle;
private NavigationDrawerCallback mCallbacks;

private DrawerLayout mDrawerLayout;
private ListView mDrawerListView;
private View mFragmentContainer;

private boolean mUserLearnedNavigation;
private static final String PREF_USER_LEARNED_NAVI = "navigation_learned";
private static final String STATE_SELECTED_POSITION = "selected_position";

private int mCurrentSelectedPosition;
private boolean mFromSavedInstanceState;

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.v("boot", "Nav - onCreate");
    super.onCreate(savedInstanceState);

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
    mUserLearnedNavigation = sp.getBoolean(PREF_USER_LEARNED_NAVI, false);

    if (savedInstanceState != null) {
        mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
        mFromSavedInstanceState = true;
    }

    selectItem(mCurrentSelectedPosition);
} ... 
private void selectItem(int position) {
    Log.v("boot", "Nav - selectitem");
    mCurrentSelectedPosition = position;
    if (mDrawerListView != null) {
        mDrawerListView.setItemChecked(position, true);
    }

    if (mDrawerLayout != null) {
        mDrawerLayout.closeDrawer(mFragmentContainer);
    }

    if (mCallbacks != null) {
        mCallbacks.onNavigationDrawerItemClicked(position);
    }

    switch (position) {
        case 0:
            startActivity(new Intent(getActivity(), MainActivity.class));
            break;
        case 1:
            startActivity(new Intent(getActivity(), ExpensesActivity.class));
            break;
        default:
            startActivity(new Intent(getActivity(), MainActivity.class));
            break;
    }
}

1 个答案:

答案 0 :(得分:1)

每次启动活动时,都会重新创建菜单,从而执行开关案例。您不应该在菜单的onCreate中调用selectItem。如果要保持用户导航,则必须在MainActivity中启动正确的活动。

您可以添加一个检查,以查看getActivity是否是您在调用startActivity之前要启动的活动的实例,因此您不会再次以无限循环运行。