How to Go Back to Previous Fragment from Activity

How to go back to previous fragment from activity?

I see, that people are still trying to help me. This answer helped me fix my problem: Android - Navigation Up from Activity to Fragment

How to go back from an activity to fragment

to be able to go back to the previous fragment should include the .addToBackStack()

if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackstack(null)
ft.commit();
}

and when you click on back button in the toolbar programmatically go back to the previous fragment using following code.

if ( getFragmentManager().getBackStackEntryCount() > 0) 
{
getFragmentManager().popBackStack();
return;
}
super.onBackPressed();

Go back to fragment from activity

I am still not clear what you want to achieve but you can try this

@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}

EDIT:

case android.R.id.home:
//call onBackPressed here
onBackPressed();
return true;

How to finish current activity and go back to previous fragment?

use start activity for result

Intent intent=new Intent(getActivity(), SkillActivity.class);
Bundle bundle = new Bundle();
bundle.putString("caller","editprofile");
bundle.putParcelableArrayList("userskills", (ArrayList<Skill>) userskills);
intent.putExtras(bundle);
startActivityForResult(intent,122);// you can change this number

and in your activity which hold the fragment add this implementation to pass the result to the fragment

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}

and in Done button

btnDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent returnIntent = new Intent();
Bundle args = new Bundle();
args.putParcelableArrayList("newskills",
(ArrayList<Skill>)newskills);
returnIntent.putExtras("result", args);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});

finaly implment this in your fragment to recive the data

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 122 && resultCode == Activity.RESULT_OK) {
Bundel bundle=data.getExtras();
String caller=data.getString("caller");
ArrayList<Skill> skills=data.getParcelable("userskills");
}
}

How do you return to the previous fragment when using a NavController?

If all that you want to do is pop the back stack, setGraph() is unnecessary. Just call popBackStack() on your NavController.

How to return to Previous Fragment by clicking back(Not Hardware back button)?

You can do it by using the popBackStack() method of FragmentManager, put this inside the onClickListener of your back button :

if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
}

If you're using the default back button of the toolbar i.e, the home button, then you can do it by placing this code in the onOptionsItemSelected() method of your Activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
}
return true;
}

return super.onOptionsItemSelected(item);
}

or, if you want the same behaviour on hardware back button press then override the onBackPressed() method in your Activity class like this:

@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}

How do I go back to previous fragment programatically with navigation component in kotlin?

You can just call popBackStack() method on your NavController, on click of that button
Something like:

findNavController().popBackStack()

For more check Navigation Component docs

Programmatically go back to the previous fragment in the backstack

Look at the getFragmentManager().popBackStack() methods (there are several to choose from)

http://developer.android.com/reference/android/app/FragmentManager.html#popBackStack()



Related Topics



Leave a reply



Submit