简体   繁体   中英

Navigating back to fragment of an activity from another activity

I am trying to navigate back to a fragment of an activity from another activity. I have set

 getActionBar().setDisplayHomeAsUpEnabled(true);

but whenever, I use the app back icon it always takes me to the first fragment of the activity rather than the fragment from which another activity was launched. How can I achieve this. I think I might have to implement

getSupportFragmentManager().popBackStack();

How can I do it?

Try adding the fragment you want to go back to manually to the backstack.

FragmentTransaction transaction = getFragmentManager().beginTransaction();
YourFragmentName myFragment = new YourFragmentName();
transaction.replace(R.id.fragment_container, myFragment);

//if you with to add it to backStack, do this, otherwise skip the line below
transaction.addToBackStack(null);
transaction.commit();

Hope this helps.

hope no a late answer might help someone later you do this


FragmentManager fragmentManager = getFragmentManager() ;
fragmentManager. beginTransaction().replace(R.id .(the i of the layout of you current acticity), new (name of your activity)) .commit() ;

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