所以,我发现一个有趣的问题。当我通过编程方式添加新的片段时,OnBackStackChangedListener中的onBackStackChanged方法会被调用两次,但它应该只调用一次。这是我的activity代码:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements FragmentManager.OnBackStackChangedListener{
private FragmentManager fragmentManager;
private Button button1;
private Button button2;
private Button button3;
private Fragment defaultFragment;
private Fragment previousFragment;
private Fragment currentFragment;
private String currentFragmentTag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFragment(new FragmentOne(), FragmentOne.TAG);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFragment(new FragmentTwo(), FragmentTwo.TAG);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFragment(new FragmentThree(), FragmentThree.TAG);
}
});
setupFragmentManager();
}
private void setupFragmentManager() {
fragmentManager = getSupportFragmentManager();
fragmentManager.addOnBackStackChangedListener(this);
FragmentOne fragmentOne = new FragmentOne();
defaultFragment = fragmentOne; // fragment for default
currentFragmentTag = FragmentOne.TAG;
currentFragment = fragmentOne;
fragmentManager.beginTransaction()
.add(R.id.fragments_container, currentFragment, currentFragmentTag)
.addToBackStack(currentFragmentTag)
.commit();
}
public void showFragment(Fragment fragment, String fragmentTag) {
previousFragment = currentFragment;
currentFragment = fragment;
fragmentManager.beginTransaction()
.hide(previousFragment)
.add(R.id.fragments_container, fragment, fragmentTag)
.addToBackStack(fragmentTag)
.commit();
}
@Override
public void onBackStackChanged() {
Log.d("MY_TAG" , "onBackStackChangedListener - " + fragmentManager.getBackStackEntryCount() );
if (fragmentManager.getBackStackEntryCount() > 0) {
String fragmentTag = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1).getName();
currentFragment = fragmentManager.findFragmentByTag(fragmentTag);
} else {
currentFragment = defaultFragment;
}
// some code...
}
@Override
public void onBackPressed() {
if (fragmentManager.getBackStackEntryCount() == 1) {
finish();
} else {
super.onBackPressed();
}
}
}
和片段类:
public class FragmentOne extends Fragment {
public static final String TAG = FragmentOne.class.getSimpleName();
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity();
View topLevelView = LayoutInflater.from(context).inflate(R.layout.fragment_layout, container, false);
TextView textView = (TextView) topLevelView.findViewById(R.id.fragment_Text);
textView.setText("This is FRAGMENT ONE");
return topLevelView;
}
}
FragmentTwo和FragmentThree与FragmentOne完全相同。
当我启动我的应用程序时,在日志中看到:
onBackStackChangedListener - 1
onBackStackChangedListener - 1
点击按钮后(显示下一个片段),我看到:
onBackStackChangedListener - 2
onBackStackChangedListener - 2
另一个:
onBackStackChangedListener - 3
onBackStackChangedListener - 3
并改变
.add(R.id.fragments_container, currentFragment, currentFragmentTag)
对于翻译
.replace(R.id.fragments_container, currentFragment, currentFragmentTag)
无法解决问题。
但这非常奇怪。为什么我添加片段后,我的OnBackStackChangedListener会多次调用自己的方法?
button.setOnClickListener(this)
。这样,你只需要在Activity中定义一个onClick
方法,使用switch语句找到所点击的视图并从单个实现中运行你的逻辑,而不是在每次需要点击行为的视图中都重新定义它。 - mwieczorek