我正在开发一个应用程序,其中每个选项卡中都有选项卡和片段。我得到它的工作,它在启动时显示我的列表视图,当我点击列表中的名称时,它转到下一个片段,但是当我点击后退按钮时它不会返回列表视图我只是得到一个空白的屏幕。我确定这是一个简单的答案,我已经发布了一些代码片段,如果需要更多,请告诉我。谢谢你的帮助。
public class HomeStackActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragmentstackctivity);
// add initial fragment, do not add to back stack, no transition animation
addFragment(new MyCommentActivity(), false, FragmentTransaction.TRANSIT_NONE);
}
void addFragment(Fragment fragment, boolean addToBackStack, int transition) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment1, fragment);
ft.setTransition(transition);
if (addToBackStack)
ft.addToBackStack(null);
ft.commit();
}
public void onBackPressed() {
FragmentManager manager = getSupportFragmentManager();
if (manager.getBackStackEntryCount() > 0)
getSupportFragmentManager().popBackStack();
else
finish();
}
}
public class nameOnClickListener implements OnClickListener{
private int position;
public nameOnClickListener(int position){
this.position=position;
}
@Override
public void onClick(View v) {
HashMap<String, String> update = new HashMap<String, String>();
update = commentList.get(position);
Log.d("Testing Click", update.get(MyCommentActivity.KEY_UID));
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor edit = sp.edit();
edit.putString("profile_id", update.get(MyCommentActivity.KEY_UID));
edit.putString("profile_type", update.get(MyCommentActivity.KEY_TYPE));
edit.commit();
addFragment(new UserProfileActivity(), true, FragmentTransaction.TRANS
答案 0 :(得分:0)
变化:
ft.replace(R.id.fragment1, fragment);
为:
ft.add(R.id.fragment1, fragment);