3

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.

gosulove
  • 1,645
  • 2
  • 26
  • 40
  • Possible duplicate of [How to implement onBackPressed() in Android Fragments?](http://stackoverflow.com/questions/5448653/how-to-implement-onbackpressed-in-android-fragments) – Bryan Jul 18 '16 at 13:59
  • 1
    Possible duplicate of [Android Fragment handle back button press](http://stackoverflow.com/questions/7992216/android-fragment-handle-back-button-press) – EpicPandaForce Jul 18 '16 at 14:04

6 Answers6

9

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();
    }
}
Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
2

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();
  • I have uploaded the coding for reference. Please have a look. It shows exactly what i mean. Please help me take a look. For example, where to place the coding ? thx! – gosulove Jul 18 '16 at 15:01
1

You just need this one single line of code!

getFragmentManager().popBackStackImmediate();
johnrao07
  • 6,690
  • 4
  • 32
  • 55
  • it doesn't work. where should i place this code? In Fragment A or B or Activity before transaction.commit();? – gosulove Jul 18 '16 at 14:26
  • I have uploaded the coding for reference. Please have a look. It shows exactly what i mean. Please help me take a look. For example, where to place the coding ? thx! – gosulove Jul 18 '16 at 15:02
1

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.

UeliDeSchwert
  • 1,146
  • 10
  • 23
  • I have uploaded the coding for reference. Please have a look. It shows exactly what i mean. Please help me take a look. For example, where to place the coding ? thx! – gosulove Jul 18 '16 at 15:02
1

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();
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • where should i place this code - getFragmentManager().popBackStack(); ? In Fragment A or B or Activity before transaction.commit();? and this matter works on clicking "Hardware Back button" only ? – gosulove Jul 18 '16 at 14:29
  • Just call it in a click listener – EpicPandaForce Jul 18 '16 at 14:30
  • I have uploaded the coding for reference. Please have a look. It shows exactly what i mean. Please help me take a look. For example, where to place the coding ? thx! – gosulove Jul 18 '16 at 15:02
1

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.