Back to back button

0

that I can use so that when I click on the back button on Android it takes me to a fragment that I choose. I mean, I'm on a screen, and clicking on the back, takes me to another one but not the previous one, but another.

Code

public class BP_ColesBruselas extends Fragment {


public BP_ColesBruselas() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
        View view;

    view = inflater.inflate(R.layout.fragment_bp__coles_bruselas, container, false);

    return view ;
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if (keyCode == event.KEYCODE_BACK) {
        getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content_main,new Bajar_Peso()).commit();
    }
    return super.onKeyDown(keyCode, event);
}

}

    
asked by Sergio 24.01.2017 в 20:33
source

2 answers

0

Something more or less so friend so you can enternder

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case android.R.id.home:
            TuFragment tufragment = new TuFragment ();
            tufragment.show(getActivity().getFragmentManager(), "tutag");
            return true;


    }
    return super.onOptionsItemSelected(item);
}
    
answered by 25.01.2017 в 17:04
0

Hello friend, I do not know much about Android, but maybe I can help you with what I will tell you.

public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == event.KEYCODE_BACK) {
    //Aqui puedes masndar llamar una funcion en tu activity principal
    //la cual infla todos los fragments
    infla_fragments("back");
}
return super.onKeyDown(keyCode, event);
}

Later in the function you can have a switch with the function

public infla_fragments(String info){
  switch (info){
    TuFragment tufragment = new TuFragment ();
    tufragment.inflate(getActivity().getFragmentManager());
  }
}

Something like that I do not understand much about Android and I have hardly worked with Android. I hope I could have helped you.

    
answered by 26.02.2017 в 01:41