简体   繁体   中英

Need to press back twice to go back to previous fragment

I'm developping an Android app which uses multiple fragments. Somehow on one of the fragments, when I press the back button once, nothing seems to happen. When I press it a second time it will bring me to the previous fragment. What could cause this behavior? I only want to have to press the back button once.

This is the code of the fragment:

public class FragmentMeerInfo extends Fragment
{
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
public SharedPreferences passedName;
private String knNaam;
public View view;

public FragmentMeerInfo()
{

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    if (savedInstanceState != null)
    {
        mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
    }
    view = inflater.inflate(R.layout.fragment_meerinfo, container, false);
    passedName = getActivity().getSharedPreferences("PASSED_NAME", 0);
    knNaam = passedName.getString("knNaam", null);

    Typeface tfreg = Typeface.createFromAsset(getActivity().getAssets(),
            "Roboto-Regular.ttf");
    Typeface tfbold = Typeface.createFromAsset(getActivity().getAssets(),
            "Roboto-Black.ttf");

    SQLClass SQLReader = new SQLClass(getActivity());
    ObjectKunstenaar kn = new ObjectKunstenaar();
    kn = SQLReader.getSingleKn(knNaam);

    TextView TvTitle = (TextView) view.findViewById(R.id.tvTitel);
    TextView TvInfo = (TextView) view.findViewById(R.id.tvInfo);
    ImageView IvImage = (ImageView) view.findViewById(R.id.imgKunstenaar);

    TvTitle.setText("Biografie " + knNaam);
    TvTitle.setTypeface(tfbold);

    TvInfo.setText(kn.getKunstenaarInfo());
    TvInfo.setTypeface(tfreg);

    Bitmap bitmap;

    final File image = new File(getActivity().getFilesDir()
            .getAbsolutePath()
            + File.separator
            + "kn"
            + File.separator
            + String.valueOf(kn.getKunstenaarId())
            + File.separator
            + "thumb.jpg");
    FileInputStream fi;

    try
    {
        fi = new FileInputStream(image);
        bitmap = BitmapFactory.decodeStream(fi);
        fi.close();
        IvImage.setImageBitmap(bitmap);
    } catch (FileNotFoundException e)
    {
        e.printStackTrace();
    } catch (IOException e)
    {
        e.printStackTrace();
    }
    return view;
}
}

I'm sure my code isn't the best but I'm still a beginner

EDIT: I already found out what I was doing wrong. I was accidentally opening the fragment twice, so it needed to be closed twice too. Thanks for the fast replies though!

I already found out what I was doing wrong. I was accidentally opening the fragment twice, so it needed to be closed twice too. Thanks for the fast replies though!

Try override OnKeyUp or OnKeyDown from your activity.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // check if user press two times and back to previous fragment!

        return true;
    }

    return super.onKeyDown(keyCode, event);
}

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