简体   繁体   中英

Android Fragment Backstack not showing

i'm developing an application where I have tabs and fragments within each tab. I got it to work where it shows my listview on launch and when I click a name in the list its goes to the next fragment, but when I hit the back button it doesn't go back to the listview I just get a blank screen. I'm sure it's a simple answer and I have posted some code snippets, if more is needed please let me know. Thank you for your help.

FragmentActivity

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

}

NameClick

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

change:

ft.replace(R.id.fragment1, fragment);

to:

ft.add(R.id.fragment1, fragment);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM