I have three fragment in an Activity C. they are behaving as tabs. I have to go from a fragment to a new Activity X. Now i want to come back to fragment from Activity X.
I have override onBackPressed but don't know how to go back to fragment/not fragmentActivity from an Activity.
if i want to go back to an Activity to another activity i override on back pressed and using intent call that actvity..
i want to do some thing like this .. this code is for coming back to previous activity
@Override
public void onBackPressed()
{
Intent intent = new Intent(CurrentActivity.this,ActivityYouLikeToGo.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
thanks in Advance..
Using addToBackStack(null) is including the fragments in the navigation, causing the "Back Button" to display the previous fragment . If you do not want to navigate backwards between the fragments open do not add transanction to backstack . Do not use addToBackStack(null) . fragmentManager.
To get the current fragment that's active in your Android Activity class, you need to use the supportFragmentManager object. The supportFragmentManager has findFragmentById() and findFragmentByTag() methods that you can use to get a fragment instance.
Bookmark this question. Show activity on this post. I see in the Android Fragments Dev Guide that an "activity can call methods in a fragment by acquiring a reference to the Fragment from FragmentManager, using findFragmentById() or findFragmentByTag() ."
Simplest way :
@Override
onBackPressed(){
finish();
}
when you start an activity from a fragment, the fragment never got killed, you can simply come back to your fragment by kill the current activity by finish();, it just like when from a fragment you open an activity and on pressing back button you jumped to the fragment without losing data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With