gpt4 book ai didi

android - ActionBar 下拉导航 - 方向更改将错误的 fragment 放入

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:17:36 25 4
gpt4 key购买 nike

我有一个操作栏下拉导航。问题是当我切换到另一个 fragment ,然后进行方向更改时,它无论如何都会将第一个 fragment 放入其中,即使我认为我正在正确处理 savedInstanceState。问题似乎是 onNavigationItemSelected 被调用,所以 .. 我将如何正确处理这个问题?我可以将 savedInstanceState 变量设为一个字段,但感觉不对......

public class MainActivity extends FragmentActivity implements MyListFragment.OnArticleSelectedListener {

public static final String TAG = "MainActivity";

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if(savedInstanceState != null) {
Fragment savedFragment = getSupportFragmentManager().getFragment(savedInstanceState, "saved_fragment");
Log.d(MainActivity.TAG, "savedInstanceState != null: " + savedFragment.getTag());

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, savedFragment, savedFragment.getTag())
.commit();
} else {

Log.d(MainActivity.TAG, "savedInstanceState == null");

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new MyListFragment(), MyListFragment.TAG)
.commit();
}

ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

String[] array = new String[] { "Inzeráty", "Dummy frag" };
SpinnerAdapter mSpinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
array);

actionBar.setListNavigationCallbacks(mSpinnerAdapter, new ActionBar.OnNavigationListener() {

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {

Log.d(MainActivity.TAG, "onNavitagionItemSelected");

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch(itemPosition) {
case 0:
transaction.replace(R.id.fragment_container, new MyListFragment(), MyListFragment.TAG);
break;
case 1:
transaction.replace(R.id.fragment_container, new MyDummyFragment(), MyDummyFragment.TAG);
break;
}
transaction.commit();
return true;
}

});

}

@Override
public void onArticleSelected(Bundle bundle) {
Log.d(MainActivity.TAG, "MainActivity # onArticleSelected");
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
Log.d(MainActivity.TAG, "MainActivity # onSaveInstanceState: " + currentFragment.getTag());
getSupportFragmentManager().putFragment(outState, "saved_fragment", currentFragment);
super.onSaveInstanceState(outState);
}

最佳答案

我最近也遇到了这个问题。我通过重写 Activity 的 onRetainCustomNonConfigurationInstance 方法解决了这个问题。

@Override
public Object onRetainCustomNonConfigurationInstance() {
// return true so that onCreate will know it is an orientation change
return true;
}

在我的 onCreate 中,我能够实现以下内容:

...
Object lastCustomNonConfigurationInstance = getLastCustomNonConfigurationInstance();
if (lastCustomNonConfigurationInstance != null) {
mIsOrientationChange = (Boolean) getLastCustomNonConfigurationInstance();
}
...

最后我更新了 onNavigationItemSelected 以便它知道 mIsOrientationChange

 @Override
public boolean onNavigationItemSelected(int position, long id) {
if (!mIsOrientationChange) {
// real navigation selected logic
}

mIsOrientationChange= false;

return true;
}

编辑:我从以下 Android 开发者文章中得到了实现它的想法:http://developer.android.com/guide/topics/resources/runtime-changes.html#RetainingAnObject

关于android - ActionBar 下拉导航 - 方向更改将错误的 fragment 放入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14145781/

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