Zar E Ahmer
Zar E Ahmer

Reputation: 34360

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

Upvotes: 12

Views: 23349

Answers (11)

user35398
user35398

Reputation: 68

You can add this in activity.xml:

<androidx.appcompat.widget.Toolbar
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/white"
        app:navigationIcon="?attr/homeAsUpIndicator" />

then in activity.kt :

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityBinding.inflate(layoutInflater)
        setContentView(binding!!.root)

        binding!!.toolBar.setNavigationOnClickListener{
            onBackPressed()
        }
}

and in the overided function do this:

override fun onBackPressed() {
        super.onBackPressed()
        finish()
    }

this will get you back to the same previous status of the fragment

Upvotes: 0

Jay Soni
Jay Soni

Reputation: 1

enter image description here

@Override
public void onBackPressed() {
  //Here "digiM" is an Actionbar Title
    if(getTitle().equals("digiM") || getSupportActionBar().getTitle().equals("digiM"))
    {
        //reload the activity if you want to open it again as your need
        Intent intent = new Intent(Your_Current_Activity.this,Your_Current_Activity.class);
        startActivity(intent);
    }
}

If you have action bar in that fragment, then you can check action bar title on onBackPressed() in Activity which contains method like.

Upvotes: 0

Mubashir Murtaza
Mubashir Murtaza

Reputation: 337

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.

Upvotes: 2

Suman Bakhati
Suman Bakhati

Reputation: 76

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

Upvotes: 0

Kunal Parikh
Kunal Parikh

Reputation: 463

Simplest way :

@Override
onBackPressed(){
finish();
}

Upvotes: 5

Thiago Pereira
Thiago Pereira

Reputation: 1712

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 :)

Upvotes: 0

rohitanand
rohitanand

Reputation: 728

viewPager.setCurrentItem(tabPosition);

This solution did wonders for me.

Upvotes: -2

upenpat
upenpat

Reputation: 685

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.

Upvotes: 0

Andy Res
Andy Res

Reputation: 16043

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.

Upvotes: 0

Soumil Deshpande
Soumil Deshpande

Reputation: 1632

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

Upvotes: 7

Arnab Bhagabati
Arnab Bhagabati

Reputation: 2715

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

Upvotes: 5

Related Questions