Android应用程序中的上一个按钮

Android应用程序中的上一个按钮,android,android-fragments,Android,Android Fragments,我正在开发一个简单的Android应用程序。我有一个带有两个按钮的片段:prev和home。如果我触摸主页,返回主菜单时效果良好,但如果我想返回上一个活动,我必须触摸两次prev按钮。我不知道发生了什么事。我找了,但什么也没找。有人有同样的问题吗 谢谢你的帮助 这是我的代码: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bun

我正在开发一个简单的Android应用程序。我有一个带有两个按钮的片段:prev和home。如果我触摸主页,返回主菜单时效果良好,但如果我想返回上一个活动,我必须触摸两次prev按钮。我不知道发生了什么事。我找了,但什么也没找。有人有同样的问题吗

谢谢你的帮助

这是我的代码:

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                       Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.navigation_activity_fragment, container, false);
TextView textView = (TextView) view.findViewById(R.id.activityTitle);
Button home = (Button) view.findViewById(R.id.home);
Button prev = (Button) view.findViewById(R.id.button2);

home.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
        Intent i = new Intent(getActivity(),  MainMenuActivity.class);
        startActivity(i);
                }

});
prev.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View arg0) {
        getActivity().finish();
                }

});


return view;
 }
和xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/RelativeLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#00000000"
            android:orientation="vertical">

<ImageView
android:id="@+id/screen_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/screen_header"
android:contentDescription="@null"/>

<TextView
  android:id="@+id/activityTitle"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignBottom="@+id/screen_header"
  android:layout_centerHorizontal="true"
  android:layout_marginBottom="14dp"
  android:text="Large Text"
  android:textAppearance="?android:textAppearanceLarge"
  android:textColor="@color/black"
  android:textSize="16.5dip"
  android:textStyle="bold" />

<Button
  android:id="@+id/home"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignBaseline="@+id/activityTitle"
  android:layout_alignBottom="@+id/activityTitle"
  android:layout_alignParentRight="true"
  android:background="@drawable/home_btn" 
  android:onClick="onClick"/>

<Button
  android:id="@+id/button2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignBottom="@+id/activityTitle"
  android:layout_alignParentLeft="true"
  android:background="@drawable/prev_btn" 
  android:onClick="onClick"/>

 </RelativeLayout>

这是一种普遍的做法-

每当需要在UI中显示片段时,都必须向事务添加片段

比如说-

从片段A转到片段B,A和B一起使用

现在从片段B开始,使用popBackStack,这将返回A

prev.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View arg0) {
        getActivity().getSupportFragmentManager().popBackStack();

                }
});
对于您的代码,尤其是不确定的代码,但这不是返回上一个片段的正确方法。执行getActivity.finish时,您正在完成当前活动,而不是返回到上一个片段。请注意,当您的每个活动都有一个片段时,您尝试的似乎是正确的,但是如果您的活动包含许多片段,并且您对其执行finish,则它会将您切换回上一个活动

因此,为了启用片段导航,您必须将它们添加到


然后回到前面的片段,只需使用。

您的问题是什么?某些UI元素保留了焦点。这就是为什么第一回似乎什么也没做,而实际上你在送出焦点,这里是碎片完成。点击后退按钮