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..
You can add a TAG (A string name for the fragment basically) and then load the fragment using findFragmentByTag().
Google documentation for findFragmentByTag()
is here.
Or you can also addToBackStack()
while calling the fragment from FragmentTransaction
, doc link here.
from google docs:
"By calling addToBackStack(), the replace transaction is saved to the
back stack so the user can reverse the transaction and bring back the
previous fragment by pressing the Back button."
First, Override the back press to goto the activity where the fragments are :-
@Override
public void onBackPressed()
{
Intent intent = new Intent(CurrentActivity.this,ActivityYouLikeToGo.class);
intent.putExtra("Check",1);
startActivity(intent);
}
then goto the ActivityYouLikeToGo.java file and in onCreate do this:-
Intent intent = getIntent();
String s1 = intent.getStringExtra("Check");
if(s1.equals("1"))
{
s1 = "";
Fragment fragment = new YOURFRAMENTNAME();
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
}
}
Simplest way :
@Override
onBackPressed(){
finish();
}
if i want to go back to an Activity to another activity i override on
back pressed and using intent call that actvity
Normally, if you want to go back you don't have to do something. Pressing the device's hardware Back button will take you to the previous activity with the corresponding tab already opened.
You can start the activity that you are navigating to using startActivityForResult() and then while coming back to the previous activity you set the result.In the activity that you are coming back to,show the fragment depending on the resultcode. You can accomplish this task by hiding all the fragments that you dont want to show. This can work even if you are going to another activity from another fragment.
In the activity you have started from your fragment, you can do the following:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//do whatever you want here
finish();
return true;
}
That works for me :)
This code works in my case. I just make an Onclick method to the button . the code is below:I made onclick method clickForFriends in Button and Id is backFriendsList, And I use android.support.v4.app.Fragment class. This method should take back from new Activity to the fragment from the Activity was launched and this method should be in the Activity after the onCreate method
public void clickedForFriends(View v){
if (v.getId() == R.id.backFriendsList){
onBackPressed();
}
}
viewPager.setCurrentItem(tabPosition);
This solution did wonders for me.