Android 由其他片段调用的Fragment onBackPressed方法

Android 由其他片段调用的Fragment onBackPressed方法,android,android-fragments,Android,Android Fragments,我对ViewPager和片段实现有点问题 我在一个ViewPager中有4个片段{A;B;C:D},其中一个是纵断面图(D片段)。当用户登录时,我有一个片段事务,它用一个详细的概要视图{D---D1}替换该概要视图。在这个详细的纵断面图视图中,我实现了一个定制的onBackPressed行为,只针对像这样的片段 this.rootView.setFocusableInTouchMode(true); this.rootView.requestFocus(); this.r

我对ViewPager和片段实现有点问题

我在一个ViewPager中有4个片段{A;B;C:D},其中一个是纵断面图(D片段)。当用户登录时,我有一个片段事务,它用一个详细的概要视图{D---D1}替换该概要视图。在这个详细的纵断面图视图中,我实现了一个定制的onBackPressed行为,只针对像这样的片段

    this.rootView.setFocusableInTouchMode(true);
    this.rootView.requestFocus();
    this.rootView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (event.getAction() != KeyEvent.ACTION_DOWN) {
                final MaterialDialog mMaterialDialog = new 
                MaterialDialog(getActivity());
                mMaterialDialog.setTitle("LOG OUT")
                        .setMessage("If you log out right now, you will not be able to make a booking")
                        .setPositiveButton("OK", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ParseUser user = ParseUser.getCurrentUser();
                                user.logOut();

                                mMaterialDialog.dismiss();

                                FragmentTransaction trans = getFragmentManager()
                                        .beginTransaction();
                                trans.replace(R.id.profile_frame, new ProfileFragment());
                                trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                                trans.commit();
                            }
                        })
                        .setNegativeButton("CANCEL", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mMaterialDialog.dismiss();
                            }
                        });
                mMaterialDialog.show();


                return true;
            }

            switch (keyCode) {
                case KeyEvent.KEYCODE_1:
                    break;
                case KeyEvent.KEYCODE_2:

                    break;
                case KeyEvent.KEYCODE_3:
                    break;

            }

            return true;

        }
    });
我的问题是,当用户单击后退按钮时(例如在片段B中),它会触发片段D1方法。为什么会这样?。这是我的主要活动和完整的D1代码片段

主要活动

    public class MainActivity extends FragmentActivity {

private TabLayout tabLayout; private LinearLayout settings, booking, about;
private Button contactUs, visitOurSite;
private DrawerLayout drawer; private LinearLayout drawerContent;
private Tracker tracker;
private Fragment musicFrag, profileDetailFrag, filterFrag, mapFrag, profileFrag, favoritesFrag;
private ViewPager viewPager;
private int[] tabIconsUnselected = {
        R.drawable.tab_home,
        R.drawable.tab_map,
        R.drawable.tab_favorites,
        R.drawable.tab_profile
};

private int[] tabIconsSelected = {
        R.drawable.tab_home_pressed,
        R.drawable.tab_map_pressed,
        R.drawable.tab_favorites_pressed,
        R.drawable.tab_profile_pressed
};

public static FragmentManager fragmentManager;
private Window window;

@Override
protected void onStart() {
    super.onStart();
    GoogleAnalytics.getInstance(this).reportActivityStart(this);
}

@Override
protected void onStop() {
    super.onStop();
    GoogleAnalytics.getInstance(this).reportActivityStop(this);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    fragmentManager = getSupportFragmentManager();

    Typeface semiBold = Typeface.createFromAsset(getAssets(), "fonts/GillSans-SemiBold.ttf");
    Typeface light = Typeface.createFromAsset(getAssets(), "fonts/GillSans-Light.ttf");
    Typeface bold = Typeface.createFromAsset(getAssets(), "fonts/GillSans-Bold.ttf");

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setPageTransformer(true, new ParallaxPagerTransformer(R.id.parallax_content));
    viewPager.setOffscreenPageLimit(1);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            switch (tab.getPosition()) {
                case 0:
                    tracker.send(new HitBuilders.EventBuilder().setCategory("Tabs").setAction("Click").setLabel("Home tab").build());
                    viewPager.setCurrentItem(tab.getPosition());
                    setPressed(tab.getPosition());

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        window = getWindow();
                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                        window.setStatusBarColor(getResources().getColor(R.color.colorSecondaryDark));
                    }

                    break;
                case 1:
                    viewPager.setCurrentItem(tab.getPosition());
                    setPressed(tab.getPosition());

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        window = getWindow();
                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                        window.setStatusBarColor(getResources().getColor(R.color.colorSecondaryDark));
                    }

                    break;
                case 2:
                    viewPager.setCurrentItem(tab.getPosition());
                    setPressed(tab.getPosition());

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        window = getWindow();
                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                        window.setStatusBarColor(getResources().getColor(R.color.colorSecondaryDark));
                    }

                    break;
                case 3:
                    viewPager.setCurrentItem(tab.getPosition());
                    setPressed(tab.getPosition());

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        window = getWindow();
                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                        window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
                    }

                    break;
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    contactUs = (Button) findViewById(R.id.contact);
    contactUs.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tracker.send(new HitBuilders.EventBuilder().setCategory("Navigation Drawer").setAction("Click").setLabel("Contact Us").build());
            startActivity(new Intent(MainActivity.this, ContactUsActivity.class));
        }
    });

    visitOurSite = (Button) findViewById(R.id.web);
    visitOurSite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("http://landing.vamospues.co");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });

    settings = (LinearLayout) findViewById(R.id.settings_button);
    TextView settingsLabel = (TextView) settings.getChildAt(1);
    settingsLabel.setTypeface(semiBold);

    settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, SettingsActivity.class));
            finish();
        }
    });

    booking = (LinearLayout) findViewById(R.id.booking_button);
    booking.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, BookingActivity.class));
        }
    });
    TextView reservationsLabel = (TextView) booking.getChildAt(1);
    reservationsLabel.setTypeface(semiBold);

    about = (LinearLayout) findViewById(R.id.about_button);
    TextView aboutLabel = (TextView) about.getChildAt(1);
    aboutLabel.setTypeface(semiBold);

    about.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tracker.send(new HitBuilders.EventBuilder().setCategory("Navigation Drawer").setAction("Click").setLabel("About").build());
            startActivity(new Intent(MainActivity.this, AboutActivity.class));
        }
    });

    setupTabIcons();
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawer.openDrawer(findViewById(R.id.drawer_content));
}

private void setPressed(int i) {
    if (i==0){
        tabLayout.getTabAt(0).setIcon(tabIconsSelected[0]);
        tabLayout.getTabAt(1).setIcon(tabIconsUnselected[1]);
        tabLayout.getTabAt(2).setIcon(tabIconsUnselected[2]);
        tabLayout.getTabAt(3).setIcon(tabIconsUnselected[3]);
    }
    if (i==1){
        tabLayout.getTabAt(0).setIcon(tabIconsUnselected[0]);
        tabLayout.getTabAt(1).setIcon(tabIconsSelected[1]);
        tabLayout.getTabAt(2).setIcon(tabIconsUnselected[2]);
        tabLayout.getTabAt(3).setIcon(tabIconsUnselected[3]);
    }
    if (i==2){
        tabLayout.getTabAt(0).setIcon(tabIconsUnselected[0]);
        tabLayout.getTabAt(1).setIcon(tabIconsUnselected[1]);
        tabLayout.getTabAt(2).setIcon(tabIconsSelected[2]);
        tabLayout.getTabAt(3).setIcon(tabIconsUnselected[3]);
    }
    if (i==3){
        tabLayout.getTabAt(0).setIcon(tabIconsUnselected[0]);
        tabLayout.getTabAt(1).setIcon(tabIconsUnselected[1]);
        tabLayout.getTabAt(2).setIcon(tabIconsUnselected[2]);
        tabLayout.getTabAt(3).setIcon(tabIconsSelected[3]);
    }
}

private void setupTabIcons() {
    tabLayout.getTabAt(0).setIcon(tabIconsSelected[0]);
    tabLayout.getTabAt(1).setIcon(tabIconsUnselected[1]);
    tabLayout.getTabAt(2).setIcon(tabIconsUnselected[2]);
    tabLayout.getTabAt(3).setIcon(tabIconsUnselected[3]);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new FilterFragment(), "ONE");
    adapter.addFrag(new MapFragment(), "TWO");
    adapter.addFrag(new FavoritesFragment(), "THREE");
    adapter.addFrag(new ProfileFragment(), "FOUR");
    viewPager.setAdapter(adapter);
}
}

片段D1

   public class ProfileDetailFragment extends Fragment {

private View rootView; RotateLoading loader;
private CircleImageView profilePic; ScrollView profileContainer;
private String id, name, mail, birthday, phone, url;
private TextView nameLabel, mailLabel, birthdayLabel, phoneLabel;
private Bitmap picture;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    if (rootView != null) {
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null)
            parent.removeView(rootView);
    }
    try {
        rootView = inflater.inflate(R.layout.profile_detail_fragment, container, false);
    } catch (InflateException e) {

    }

    if(ParseUser.getCurrentUser() == null){
        FragmentTransaction trans = getChildFragmentManager()
                .beginTransaction();
        trans.replace(R.id.profile_frame, new ProfileFragment());
        trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        trans.commit();
    }

    loader = (RotateLoading) rootView.findViewById(R.id.loading);
    profileContainer = (ScrollView) rootView.findViewById(R.id.profile_container);
    profileContainer.setVisibility(View.GONE);

    nameLabel = (TextView) rootView.findViewById(R.id.name_label);
    mailLabel = (TextView) rootView.findViewById(R.id.mail_label);
    birthdayLabel = (TextView) rootView.findViewById(R.id.birthday_label);
    phoneLabel = (TextView) rootView.findViewById(R.id.phone_label);
    profilePic = (CircleImageView) rootView.findViewById(R.id.profile_pic);

    this.rootView.setFocusableInTouchMode(true);
    this.rootView.requestFocus();
    this.rootView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (event.getAction() != KeyEvent.ACTION_DOWN) {
                final MaterialDialog mMaterialDialog = new MaterialDialog(getActivity());
                mMaterialDialog.setTitle("LOG OUT")
                        .setMessage("If you log out right now, you will not be able to make a booking")
                        .setPositiveButton("OK", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ParseUser user = ParseUser.getCurrentUser();
                                user.logOut();

                                mMaterialDialog.dismiss();

                                FragmentTransaction trans = getFragmentManager()
                                        .beginTransaction();
                                trans.replace(R.id.profile_frame, new ProfileFragment());
                                trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                                trans.commit();
                            }
                        })
                        .setNegativeButton("CANCEL", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mMaterialDialog.dismiss();
                            }
                        });
                mMaterialDialog.show();


                return true;
            }

            switch (keyCode) {
                case KeyEvent.KEYCODE_1:
                    break;
                case KeyEvent.KEYCODE_2:

                    break;
                case KeyEvent.KEYCODE_3:
                    break;

            }

            return true;

        }
    });

    return rootView;
}


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

    new RemoteDataTask().execute();
    super.onViewCreated(view, savedInstanceState);
}

public class RemoteDataTask extends AsyncTask<Void,Void,Void>
{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        loader.start();

    }

    @Override
    protected Void doInBackground(Void... params) {
        ParseUser user = ParseUser.getCurrentUser();
        id = user.get("id").toString();
        name = user.get("name").toString();
        mail = user.getEmail();
        birthday = user.get("birthday").toString();
        phone = user.get("phone").toString();
        url = user.get("pic_url").toString();

        return null;
    }


    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        loader.stop();
        profileContainer.setVisibility(View.VISIBLE);
        Picasso.with(getActivity()).load(url).into(profilePic);
        birthdayLabel.setText(birthday);
        mailLabel.setText(mail);
        nameLabel.setText(name);
        phoneLabel.setText(phone);

    }
}
公共类ProfileDetailFragment扩展了片段{
私有视图rootView;旋转加载程序;
private CircleImageView profilePic;ScrollView profileContainer;
私人字符串id、姓名、邮件、生日、电话、url;
私有文本视图名称标签、邮件标签、生日标签、电话标签;
私有位图图片;
@可空
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
if(rootView!=null){
ViewGroup parent=(ViewGroup)rootView.getParent();
如果(父项!=null)
parent.removeView(rootView);
}
试一试{
rootView=充气机。充气(R.layout.profile\u detail\u片段,容器,假);
}锁扣(充气异常e){
}
if(ParseUser.getCurrentUser()==null){
FragmentTransaction=getChildFragmentManager()
.beginTransaction();
trans.replace(R.id.profile_框架,新的ProfileFragment());
trans.setTransition(FragmentTransaction.transition\u FRAGMENT\u OPEN);
trans.commit();
}
loader=(RotateLoading)rootView.findViewById(R.id.loading);
profileContainer=(ScrollView)rootView.findViewById(R.id.profile\u容器);
profileContainer.setVisibility(View.GONE);
namelab=(TextView)rootView.findViewById(R.id.name\u标签);
mailLabel=(TextView)rootView.findViewById(R.id.mail\u标签);
birthdayLabel=(TextView)rootView.findViewById(R.id.birthday\u标签);
phoneLabel=(TextView)rootView.findViewById(R.id.phone\u label);
profilePic=(CircleImageView)rootView.findViewById(R.id.profile_pic);
this.rootView.setFocusableInTouchMode(true);
this.rootView.requestFocus();
this.rootView.setOnKeyListener(新视图.OnKeyListener(){
@凌驾
公共布尔onKey(视图v、int keyCode、KeyEvent事件){
if(event.getAction()!=KeyEvent.ACTION\u向下){
最终材料对话框mMaterialDialog=新材料对话框(getActivity());
mMaterialDialog.setTitle(“注销”)
.setMessage(“如果您现在注销,您将无法进行预订”)
.setPositiveButton(“确定”,新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
ParseUser=ParseUser.getCurrentUser();
user.logOut();
mMaterialDialog.disclose();
FragmentTransaction=getFragmentManager()
.beginTransaction();
trans.replace(R.id.profile_框架,新的ProfileFragment());
trans.setTransition(FragmentTransaction.transition\u FRAGMENT\u OPEN);
trans.commit();
}
})
.setNegativeButton(“取消”,新建视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
mMaterialDialog.disclose();
}
});
mMaterialDialog.show();
返回true;
}
开关(钥匙代码){
案例KeyEvent.KEYCODE_1:
打破
案例KeyEvent.KEYCODE_2:
打破
案例KeyEvent.KEYCODE_3:
打破
}
返回true;
}
});
返回rootView;
}
@凌驾
已创建视图上的公共void(视图,@Nullable Bundle savedInstanceState){
新建RemoteDataTask().execute();
super.onViewCreated(视图,savedInstanceState);
}
公共类RemoteDataTask扩展了AsyncTask
{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
loader.start();
}
@凌驾
受保护的Void doInBackground(Void…参数){
ParseUser=ParseUser.getCurrentUser();
id=user.get(“id”).toString();
name=user.get(“name”).toString();
mail=user.getEmail();
生日=user.get(“生日”).toString();
phone=user.get(“phone”).toString();
url=user.get(“pic_url”).toString();
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
loader.stop();
profileContainer.setVisibility(View.VISIBLE);
Picasso.with(getActivity()).load(url).into(profilePic);
生日标签.setText(生日);
mailLabel.setText(邮件);
nameLabel.setText(名称);
phoneLabel.setText(电话);
}
}
}


感谢您的帮助:)

您正在创建一个结构非常奇怪的项目。在任何情况下,您实际上不是在处理反按事件,而是在处理D1中的任何类型的按键。这是一个问题,但另一个问题是,您的所有片段同时存在,即使您只能看到其中一个片段。您需要做的是正确地处理onBackPressed()(该方法存在于活动上,而不是片段上)

@Override
public void onBackPressed() {
    //NOTE: I havent checked if getSelectedPosition exists, so track it however you want
    if ( viewPager.getSelectedPosition() == 3 ) {
        ((ProfileFragment) profileFragment).onBackPressed();
    } else {
        super.onBackPressed(); 
    }
}
public void onBackPressed() {
    final MaterialDialog mMaterialDialog = new MaterialDialog(getActivity());
    ...
    mMaterialDialog.show();
}