如何处理多个片段onbackpressed

时间:2017-08-31 11:00:02

标签: android fragment

主要活动有底部导航,其中包含5个片段。每个片段内部都有多个片段。如何处理它中的onbackpressed。

Homepage.java

public class Homepage extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;
        switch (item.getItemId()) {
            case R.id.home:
                selectedFragment = Fragment_home.newInstance();
                break;
            case R.id.eventsfeed:
                selectedFragment = Fragment_eventsfeed.newInstance();
                break;
            case R.id.events:
                selectedFragment = Fragment_events.newInstance();
                break;
            case R.id.messages:
                selectedFragment = Fragment_messages.newInstance();
                break;
            case R.id.settings:
                selectedFragment = Fragment_settings.newInstance();
                break;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.content, selectedFragment);
        transaction.commit();
        return true;

    }

};


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

    Fragment fragmentnewview = new Fragment_home();
    FragmentManager frMan = getSupportFragmentManager();
    FragmentTransaction frTr = frMan.beginTransaction();
    frTr.add(R.id.content,fragmentnewview);
    frTr.commit();

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    BottomNavigationViewHelper.disableShiftMode(navigation);

}}

Fragment_home.java

public class Fragment_home extends Fragment {

public static Fragment_home newInstance(){
        Fragment_home fragment=new Fragment_home();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view=inflater.inflate(R.layout.fragment_home, container, false);
    CardView card=(CardView)view.findViewById(R.id.next_page_home_card);

    card.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            view.setVisibility(View.GONE);
            Fragment fragmentnewview = new Event_details();
            FragmentManager frMan = getActivity().getSupportFragmentManager();
            FragmentTransaction frTr = frMan.beginTransaction();
            frTr.add(R.id.content,fragmentnewview);
            frTr.commit();
        }
    });


    return view;
}

}

在Fragment_home.java里面我点击它时有一个卡片视图,它转到下一个片段Event_Details.java

Event_Details.java

public class Event_details extends Fragment {
Button add_comments;
ImageButton back;
public static Event_details newInstance(){
    Event_details fragment=new Event_details();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view=inflater.inflate(R.layout.event_details, container, false);
    add_comments=(Button)view.findViewById(R.id.add_comment);

    back=(ImageButton)view.findViewById(R.id.back);

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            view.setVisibility(View.GONE);
            Fragment fragmentnewview = new Fragment_home();
            FragmentManager frMan = getActivity().getSupportFragmentManager();
            FragmentTransaction frTr = frMan.beginTransaction();
            frTr.add(R.id.content,fragmentnewview);
            frTr.commit();
        }
    });
    add_comments.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            view.setVisibility(View.GONE);
            Fragment fragmentnewview = new Comments();
            FragmentManager frMan = getActivity().getSupportFragmentManager();
            FragmentTransaction frTr = frMan.beginTransaction();
            frTr.add(R.id.content,fragmentnewview);
            frTr.commit();
        }
    });


    return view;
}

}

我在Event_Details.java中有一个按钮,当它被点击时它调用另一个片段Comments.java

Comments.java

public class Comments extends Fragment {
ImageButton back;
public static Comments newInstance(){
    Comments fragment=new Comments();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view=inflater.inflate(R.layout.add_your_comments, container, false);
    back=(ImageButton)view.findViewById(R.id.back);

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            view.setVisibility(View.GONE);
            Fragment fragmentnewview = new Event_details();
            FragmentManager frMan = getActivity().getSupportFragmentManager();
            FragmentTransaction frTr = frMan.beginTransaction();
            frTr.add(R.id.content,fragmentnewview);
            frTr.commit();
        }
    });


    return view;
}

}

back.setOnClickListener是要返回的图像按钮,但我无法在硬件后退按钮中实现。

3 个答案:

答案 0 :(得分:0)

加载片段时,请在FragmentTransaction上使用addToBackStack(null)。如果这样做,后退按钮将反转交易。

答案 1 :(得分:0)

您需要@Override您的活动onBackPressed()方法! 关于你的问题有很多很好的信息和答案!如果你想成为优秀的程序员,你需要良好的研究技巧! :)) 尝试自己研究和解决问题。写一些代码,如果你仍然无法解决问题我会给你代码进行复制和粘贴:)

答案 2 :(得分:0)

@Override onBackPressed() method as @L.Petrosyan told, inside onBackPressed()get the current position of your viewpager using viewPager.getCurrentItem(); it will return you an int`值。使用该值来管理您的代码。