简体   繁体   English

需要按两次返回上一个片段

[英]Need to press back twice to go back to previous fragment

I'm developping an Android app which uses multiple fragments.我正在开发一个使用多个片段的 Android 应用程序。 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.尝试从您的活动中覆盖OnKeyUpOnKeyDown

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按下按钮如何返回上一个片段 - How to go back to the previous Fragment on Press of a button 启动一个包含片段的活动,但是需要按两次物理“后退”按钮以返回 - Start an Activity which holds an Fragment, but need to press physical Back button twice to go back 按“后退”按钮时片段无法返回到先前的状态 - fragment can not go back to previous state when press Back button 返回上一个片段 - Go back to previous fragment 按下返回时的Android底部导航栏转到上一个片段 - Android bottom navigation bar when press back go to previous fragment 当按返回按钮不更新FragmentAdapter时转到上一个片段 - Go to previous fragment when press back button doesnt update FragmentAdapter recyclerview jetpack 错误 - 按两次后退按钮转到上一个片段 - recyclerview jetpack bug - pressing back button twice to go to previous fragment go 按返回时返回第一个片段 - go back to first fragment when press back 将一个片段替换为另一个片段,返回到工具栏上的上一个片段 - Replace one fragment with another, go back to previous fragment on toolbar back button press 按一次返回按钮停留在同一个片段上,如果按两次,它将 go 回到上一个片段 - Pressing back button once stay on the same fragment, and if pressing twice, it will go back to previous fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM