我正在开发一个使用多个片段的Android应用。不知怎的,在其中一个片段上,当我按下后退按钮一次时,似乎没有任何事情发生。当我第二次按它时,它会将我带到上一个片段。什么可能导致这种行为?我只想按一下后退按钮。
这是片段的代码:
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;
}
}
我确定我的代码不是最好的,但我还是初学者
编辑:我已经知道我做错了什么。我不小心打开了两次碎片,所以它也需要关闭两次。谢谢你的快速回复!答案 0 :(得分:1)
我已经发现我做错了什么。我不小心打开了两次碎片,所以它也需要关闭两次。谢谢你的快速回复!
答案 1 :(得分:0)
尝试覆盖活动中的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);
}