gosulove
gosulove

Reputation: 1645

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

I have only 1 activity with 2 Fragments within it. When start the activity, Fragment A will launch. After user key in sth and click the button will direct to Fragment B. So what I want to do is that by adding a "Back Arrow" sth like this <- at the top left hand corner.

Does it has to do with FragmentManager.popBackStack()? Please advice ! Thx

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FragmentManager manager=getSupportFragmentManager();
    FragmentTransaction transaction=manager.beginTransaction();
    first first=new first();
    transaction.add(R.id.top,first);
    transaction.commit();
}
}

first.java

public class first extends Fragment implements View.OnClickListener{

Button get_button;
EditText get_input_name;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_first,container,false);
    get_input_name=(EditText)rootView.findViewById(R.id.input_name);
    get_button=(Button)rootView.findViewById(R.id.submit);
    get_button.setOnClickListener(this);
    return rootView;
}

public void onClick(View v){

    FragmentManager manager=getFragmentManager();
    FragmentTransaction transaction=manager.beginTransaction();
    two Two=new two();
    Bundle bundle = new Bundle();
    bundle.putString("input_name_value",get_input_name.getText().toString());
    Two.setArguments(bundle);
    transaction.replace(R.id.top,Two);
    transaction.commit();

}
}

two.java

public class two extends Fragment implements View.OnClickListener {
TextView get_display_input;
ImageView get_back_button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_two, container, false);
}

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    get_display_input=(TextView)getActivity().findViewById(R.id.display_input);
    get_back_button=(ImageView) getActivity().findViewById(R.id.back_button);
    Bundle bundle = getArguments();
    String get_name = bundle.getString("input_name_value");
    //int store_get_input=Integer.parseInt(get_name);
    get_display_input.setText("You have entered "+get_name);
}

public void onClick(View v){

    //  WHAT TO DO HERE IN ORDER TO RETURN TO PREVIOUS Fragment when clicking the button?

}
}

enter image description here

To be more precise, please refer to the screenshot.

Upvotes: 3

Views: 18058

Answers (6)

Abhishek Charismatic
Abhishek Charismatic

Reputation: 366

Well I know I am too late to answer this but you can just call getActivity.onBackpress(); method on back click of that back icon.

Upvotes: 1

EpicPandaForce
EpicPandaForce

Reputation: 81539

In your Activity to handle hardware back button:

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

So for button click method:

getFragmentManager().popBackStack();

Upvotes: 1

Ashish Ranjan
Ashish Ranjan

Reputation: 5543

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

Upvotes: 9

UeliDeSchwert
UeliDeSchwert

Reputation: 1156

Yes, it has to do with FragmentManager.popBackStack();

Take a look here, there are plenty of methods: https://developer.android.com/reference/android/app/FragmentManager.html#popBackStack

For me, getFragmentManager().popBackStackImmediate(); made exactly that.

Upvotes: 1

johnrao07
johnrao07

Reputation: 6908

You just need this one single line of code!

getFragmentManager().popBackStackImmediate();

Upvotes: 1

Simon Phoenix
Simon Phoenix

Reputation: 195

Check out the training docs here -- they should give you a good start.

// Works with either the framework FragmentManager or the
// support package FragmentManager (getSupportFragmentManager).
getSupportFragmentManager().beginTransaction()
                           .add(detailFragment, "detail")
                           // Add this transaction to the back stack
                           .addToBackStack()
                           .commit();

Upvotes: 2

Related Questions