gpt4 book ai didi

android - 在 Activity 之间切换 -> 无限循环

转载 作者:行者123 更新时间:2023-11-30 03:09:46 26 4
gpt4 key购买 nike

我是 android 的新手,有一个“小”问题。

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

当我使用 fragment 时它工作正常但我想使用 Activity ......希望有人能帮助我:)

这是我的代码。

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;
}
}

最佳答案

每次您开始一项 Activity 时,您的菜单都会重新创建,因此 switch case 会被执行。您不应该在菜单的 onCreate 中调用 selectItem。如果您想保留用户导航,则必须在 MainActivity 中启动适当的 Activity 。

您可以在调用 startActivity 之前添加检查以查看 getActivity 是否是您要启动的 Activity 的实例,这样您就不会再次在无限循环中运行。

关于android - 在 Activity 之间切换 -> 无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21176450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com