Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get back to a fragment from an Activity

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..

like image 330
Zar E Ahmer Avatar asked May 29 '14 06:05

Zar E Ahmer


People also ask

How do I go back from fragment to activity?

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.

How do I find a fragment in an activity?

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.

Can I call fragment method from activity?

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() ."


2 Answers

Simplest way :

@Override
onBackPressed(){
finish();
}
like image 120
Kunal Parikh Avatar answered Oct 25 '22 03:10

Kunal Parikh


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.

like image 28
Mubashir Murtaza Avatar answered Oct 25 '22 01:10

Mubashir Murtaza