[英]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.尝试从您的活动中覆盖OnKeyUp
或OnKeyDown
。
@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.