简体   繁体   中英

Go back to a Fragment from an Activity onBackpress

I Tried the following code to move from a Fragment extends Fragment to an Activity extends activity.

 Intent mIntent = new Intent(getActivity(), UserProfile.class);
            Bundle bundle = new Bundle();
            bundle.putBoolean(Constant.isFromChatScreen, false);
            mIntent.putExtras(bundle);
            startActivity(mIntent);

In BaseActivity.java I have Fragment A , Fragment B , Fragment C ..from Fragment A I moved to Userprofile.java now I want to move back to Fragment A onBackPressed How can I do that?

Check Google documentation for findFragmentByTag() here .

You can also use addToBackStack() while calling the fragment from FragmentTransaction

进行片段事务时,只需在提交前使用addToBackStack(null) ,然后按返回按钮即可返回片段。

Old question, but anyone looking for an answer to this can just do this:

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

finish() "closes" the current Activity and takes you back to the Fragment (Actually, it takes you back to the Activity hosting the Fragment)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM